Binh Le
Binh Le

Reputation: 363

Is there a way to define the setter and getter method for internal properties in Objective-C?

As I know, for public properties in header file, the complier automatically generate the getter and setter methods and you're able to override these accessors. But for internal properties that declared in class extension, I don't know how to define the getter and setter methods for them.

My habit is to typically initialize an instance (if needed) and set default values for property in getter method. So, I'm looking for a way to do the same thing with internal properties.

Upvotes: 1

Views: 181

Answers (1)

Rob Napier
Rob Napier

Reputation: 299643

There is absolutely no runtime difference between "public" and "internal" properties. The difference you're discussing is just what file they're declared in. If the compiler can't see the definition of the property, then it'll give a warning (error if you're using dot notation) when you try to access it. But this has nothing to do with how anything actually works at runtime.

You can override "internal" properties exactly the same as "public" ones.

That said, and unrelated, and merely an opinion to take or leave based on experience seeing this before:

My habit is to typically initialize an instance (if needed) and set default values for property in getter method. So, I'm looking for a way to do the same thing with internal properties.

I probably wouldn't make this a "habit." It can make sense to override getters with lazy initialization for specific cases where it's important, but setting defaults in the getter this way tends to be over-complicated and bug-prone for little value. Just set the default values in the initializer.

Upvotes: 1

Related Questions