constructor - Swift Class is not constructible -


i'm following along ibook swift programming, getting error when try contruct class var. here stuct , class:

struct resolution {     var width = 0     var height = 0 }  class videomode {     var resolution = resolution()     var interlaced = false     var framerate = 0.0     var name: string? } 

i can create instance of resolution struct fine, can't seem make 1 videomode class.

    var r = resolution()     println("width:\(r.width) height:\(r.height)")     r.height = 1234     r.width = 9877     println("width:\(r.width) height:\(r.height)")     var vm = videomode() //says 'videomode' not constructible ()     let vm = videomode() //apparently works though.... why?     vm.resolution.width = 22222     vm.resolution.height = 1234     vm.name = "calimari"     print(vm) 

i find strange can explain?

update: apparently works ok in playground. not running in playground. running using master detail template using swift code. added "var vm = videomode()" in viewdidload method , error. seems ok if change "let". no clue why makes difference.

enter image description here

if don't define default values stored properties must define init().

var name: string?    // there's no default value here.     either set name `nil` or define init() 

exerpt swift documentation:

classes , structures must set of stored properties appropriate initial value time instance of class or structure created. stored properties cannot left in indeterminate state.

you can set initial value stored property within initializer, or assigning default property value part of property’s definition. these actions described in following sections.


addendum:

as stressed user @valfer, i've found following:

optional property types

[...] properties of optional type automatically initialized value of nil, indicating property deliberately intended have “no value yet” during initialization.

i ignore if above present get-go or if added after fact language in beta @ time of writing , still in flux.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -