Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

objective c - Hiding properties from public access

I'm trying to declare properties that are for internal use only in a Private category as such:

@interface BarLayer (Private)

@property (readwrite, retain) MenuItemFont  *menuButton;
@property (readwrite, retain) Menu          *menuMenu;
@property (readwrite, retain) LabelAtlas    *messageLabel;

@end

Now I'm trying to figure out where exactly I'm supposed to @synthesize those.

I tried:

@implementation BarLayer (Private)

@synthesize menuButton      = _menuButton;
@synthesize menuMenu        = _menuMenu;
@synthesize messageLabel    = _messageLabel;

@end

Here, the compiler complains:

@synthesize not allowed in a category's implementation

So I tried putting it in my BarLayer implementation, but here it doesn't find the declarations in the BarLayer interface.

no declaration of property ‘menuButton’ found in the interface

What would the correct way be?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can't use @synthesize with a category.

You can do this with an class extension (a.k.a. anonymous category), which is just a category without a name whose methods must be implemented in the main @implementation block for that class. For your code, just change "(Private)" to "()" and use @synthesize in the main @implementation block along with the rest of your code for that class.

See the Apple docs on extensions for more about that. (Apparently this is new in Mac OS 10.5.)

EDIT: An example:

// Main interface (in .h)
@interface Foo : NSObject
- (void)bar;
@end

// Private interface (in .m, or private .h)
@interface Foo ()
@property (nonatomic, copy) NSString *myData;
@end

@implementation Foo
@synthesize myData; // only needed for Xcode 4.3 and earlier
- (void)bar { ... }
@end

Another solution, which is much more work, is to use objc_setAssociatedObject and objc_getAssociatedObject to fake additional instance variables. In this case, you could declare these as properties, and implement the setters and getters yourself using the objc_* runtime methods above. For more detail on those functions, see the Apple docs on Objective-C runtime.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...