drekka
drekka

Reputation: 21903

Setting up KVO to watch properties in init methods

I have a class with a large number of init... methods (UITableViewCell). My extension has an extra property which when set, will adjust various cell properties such as colours. I had two options for handling the change to the property.

  1. Override the property setter, store the passed value and then set the colours etc.
  2. Override all the inits and add a KVO watch on the property that calls a private method to setup the colours etc.

Item #1 seems to be the simplest to apply, but means I have to deal with manually putting in the setter code (retains involved) as per Apples guidelines.

Item #2 sounds nice initially because I don't have to add the setter boiler place, but would cost more code because I would have to override all the inits.

Is there any other alternatives?

Upvotes: 0

Views: 361

Answers (1)

Lily Ballard
Lily Ballard

Reputation: 185861

You only have to override the designated initializer(s). Typically, most classes only have one designated initializer, and classes which conform to <NSCoding> have two (the second being -initWithCoder:).

Don't forget, if you set up KVO in init, you'll have to remember to unregister it in -dealloc. Objects don't automatically unregister KVO when they go away, and if you forget then whatever object that gets allocated at the same address in the future will inherit the leaked KVO observers.

Upvotes: 2

Related Questions