Blog

When property observers get called [Swift]

Published 1 min read

struct Notification { let postImageURL: String? let postID: String? var userIsFollowed = false }

Say we have a Notification struct.

var notifications = [Notification]() { didSet { tableView.reloadData() } }

Using this struct, I made an array that has a property observer.

The property observer is called in all three of the following situations.

self.notifications = notifications
notifications[index] = Notification()​
notifications[index].userIsFollowd = isFollowed
  1. It's called when you change the variable that has the property observer itself.

  2. It's called when you change a single element of the array that has the property observer.

  3. It's called when you change a property of that element.

It seems like usually only case 1 is explained, so I wrote this up.

Also, it's off topic, but it's good to remember that a property observer is called on the thread that changes the property.

Comments

Leaving a comment stores your nickname (or GitHub profile info). Privacy policy

GitHub