Is it possible to create private property in Objective-C? I do know that a kind of private property functionality could be implemented in another way but I'm interested in particular question. Thanks.
+1
A:
Yes, you can, but the code will looks a little bit strange. And it will only give you some warning if you call, you have to check for the warnings yourself
in your implementation file .m
@interface YourObject ()
@property (nonatomic, retain) NSMutableArray *infoArray;
@end
vodkhang
2010-09-08 08:07:28
And even this solution won't create true private property since it will be accessible for external code. Even without a warning if called though setValue:forKey:
Gobra
2010-09-08 09:44:02
What do you mean by that? if I call like YourObject *obj = //alloc init; then obj.infoArray, it will give me warning. You are doing KVO?
vodkhang
2010-09-08 09:46:51
@vodkhang: Why do you say the code will look strange? It looks fairly normal to me.
JeremyP
2010-09-08 09:53:25
@Gobra: -setValue:forKey: will not generate a compile time warning at all for any property private, public or even non existent.
JeremyP
2010-09-08 09:55:16
@JeremyP : I am not so sure if anybody does it or not. The first time I try, it looks strange because like you create a category without any name:)
vodkhang
2010-09-08 09:56:35
@vodkhang: Thanks, that's the point! I've just worked with pseudo private methods and missed the fact that private properties could be declared in the same way as well. Thanks!
NR4TR
2010-09-09 07:46:20
@NR4TR: properties are just a pair of methods. @vodkhang: Yes, it's a standard thing called a class extension. http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocCategories.html
JeremyP
2010-09-09 08:29:35
@JeremyP: I do know the fact that property is a pair of getter and setter, I've meant just some abstract pseudo private class. Nevertheless thank for the knowledge-refreshing link :)
NR4TR
2010-09-09 11:58:34