DerrickHo328
DerrickHo328

Reputation: 4886

Swift Class is not constructible

I'm following along with the iBook for swift programming, but I am getting an error when I try to contruct a class with var. Here is a stuct and a 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 an instance of the Resolution struct just fine, but I can't seem to make one for 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 that 'VideoMode' is not constructible with ()
    let vm = VideoMode() //Apparently this works though.... why?
    vm.resolution.width = 22222
    vm.resolution.height = 1234
    vm.name = "Calimari"
    print(vm)

I find this strange can anyone explain?

Update: Apparently it works ok in playground. I am not running this in playground. I am running it using the master detail template using swift code. I added the "var vm = VideoMode()" in the viewDidLoad method and I get an error. But it seems to be ok if I change it to "let". No clue why that makes a difference.

enter image description here

Upvotes: 3

Views: 6733

Answers (3)

user1040049
user1040049

Reputation:

If you don't define default values for all stored properties then you must define init().

var name: String?    // There's no default value here.     Either set name to `nil` or define init()

Exerpt from the Swift Documentation:

Classes and structures must set all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created. Stored properties cannot be left in an indeterminate state.

You can set an initial value for a stored property within an initializer, or by assigning a default property value as part of the property’s definition. These actions are described in the following sections.


Addendum:

As stressed by user @valfer, I've found the following:

Optional Property Types

[...] Properties of optional type are automatically initialized with a value of nil, indicating that the property is deliberately intended to have “no value yet” during initialization.

I ignore if the above was present from the get-go or if it was added after the fact as the language is in beta at the time of this writing and is still in flux.

Upvotes: 2

user3734381
user3734381

Reputation: 1

I guess you forget to put : NSObject

like this:

class VideoMode: NSObject {
    var resolution = Resolution()
    var interlaced = false
    var frameRate = 0.0
    var name: String?
}

Upvotes: -1

DerrickHo328
DerrickHo328

Reputation: 4886

Apparently the Question mark at the end of the "name" variable declaration was preventing this from constructing.

//Implementation file of VideoMode
class VideoMode {
    var resolution = Resolution()
    var interlaced = false
    var frameRate = 0.0
    var name: String = "" //removed the question mark 
}

//.....in another class
var vm = VideoMode(); //seems to work after making the above changes to the class declaration

Upvotes: 2

Related Questions