Domanda

What is the point of using the @private directive in Xcode when you are working alone or with a team that can access all properties, ivars of an app?

Is there any advantage like memory usage, performance, etc? or is this just a stuff for large teams where one part should not be using stuff that can change in the future?

thanks

È stato utile?

Soluzione

Using @private in the @interface of a class declaration ensures that no other class has access to the ivars. This is important for encapsulation. And this is just as important for a single developer as it is for a team of dozens of developers.

Encapsulation ensures you only use the provided API for a class without the need to know how it works internally. Some time down the road you may wish to change the implementation. This should have no effect on any classes already using the class. The aPI won't change, just its implementation.

Remember, ivars should only be accessed within the implementation of the given class. No other class should have access to those ivars. Otherwise your code becomes very fragile and difficult to maintain.

Edit: With modern Objective-C you shouldn't even have any ivars in the .h file anymore. Move them to the @implementation line where they are guaranteed to be private and no one using the .h file even knows what ivars you have.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top