1

xcode version: 7.3.1

Hi all,

I am following along with the latest Stanford Uni iOS course. In one of the apps that is being created, we create a struct which has a number of properties.

Because it's a struct I get a free initializer which I am calling in my controller. I am then making struct instance be a computed property so that didSet gets called.

Here's the struct

struct FacialExpression
{
var eyes: Eyes = Eyes.Open
var eyeBrows: EyeBrows = EyeBrows.Normal
var mouth: Mouth = Mouth.Smile

enum Eyes: Int {
    case Open
    case Closed
    case Squinting
}

enum EyeBrows: Int {
    case Relaxed
    case Normal
    case Furrowed

    func moreRelaxedBrow() -> EyeBrows {
        return EyeBrows(rawValue: rawValue - 1) ?? .Relaxed
    }
    func moreFurrowedBrow() -> EyeBrows {
        return EyeBrows(rawValue: rawValue + 1 ) ?? .Furrowed
    }
}

enum Mouth: Int {
    case Frown
    case Smirk
    case Neutral
    case Grin
    case Smile

}
}

Here's the controller which is creating an instance of the struct

class FaceController: UIViewController {

var expression = FacialExpression(eyes: .Open, eyeBrows: .Normal, mouth: .Smile) {

    didSet {
        updateUI()
    }

}
}

This controller is the default and only controller in the app and the init line does get called.

didSet() never gets called.

Can anyone explain why?

Stephen
  • 762
  • 1
  • 12
  • 32

2 Answers2

4

Property observers doesn't get called during the initialization.

Take a look at this question.

Community
  • 1
  • 1
Viktor Simkó
  • 2,607
  • 16
  • 22
2

Your didSet does not know what variable to watch so the proper declaration is

var expression = FacialExpression(eyes: .Open, eyeBrows: .Normal, mouth: .Smile) {

    didSet(expression) {
        updateUI()
    }

}
Trouner
  • 316
  • 2
  • 13
  • Although Viktors answer worked, this is the best answer because it identified what I did wrong rather than worked around it. – Stephen Jul 27 '16 at 12:09