Initializing Swift Optional in Class Method

75 Views Asked by At

New at Swift and playing around with optionals and how they work. Is there some reason you can't initialize an optional in a struct method? If you change 'A' to be a class then this all works. Using Xcode 6.3.1.

struct A {
    var myString: String?

    init () {
        // do something
    }

    func getData () {
        // This next line doesn't compile correctly
        //self.myString = "This is a test"
    }    
}

var a = A()
a.myString = "Test" // This line works
0

There are 0 best solutions below