views:

73

answers:

1

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
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
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
@vodkhang: Why do you say the code will look strange? It looks fairly normal to me.
JeremyP
@Gobra: -setValue:forKey: will not generate a compile time warning at all for any property private, public or even non existent.
JeremyP
@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
@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
@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
@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