Stephen Stephen - 1 year ago 115
Swift Question

struct default initialiser not triggering didSet

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 {


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?

Answer Source

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) {

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download