简体   繁体   中英

objective-C how to declare private property for category?

I'm new to objective-C, so apologies if this is repeated somewhere. I have a category(?) that is something like:

inside SomeClass.h :

@interface SomeClass (SomeCategory) <SomeDelegate>
@property (nonatomic, retain) id somePublicProperty;
@property (nonatomic, retain) id someProperty; // <-- i want to move this to "private"
@end

and now in my SomeClass.m , all i have is:

@implementation SomeClass (SomeCategory)

// dynamic setters/getters here for someProperty.

@end

I think the someProperty is public. how do i make this "private"? (in other words, how do i syntactically put this in the .m file? i tried to use

@interface SomeClass (SomeCategory) {
    @property (nonatomic, retain) somePrivateProperty;
} 
@end

but it just complains that i have duplicate definition of the category. how do i do this correctly?

In your .h file, you should not give the category. Just use:

@interface SomeClass : SomeBaseClass < SomeDelegate>
    @property (nonatomic, retain) id somePublicProperty;
@end

In your .m file, define your private property inside a class extension :

@interface SomeClass ()
    @property (nonatomic, retain) id somePrivateProperty;
@end

A class extension is not a like category in that it allows you to extend an interface as well as add new storage to your class.

In a class category, you can define new properties, but no storage will be allocated for it , so you have to do it by hand:

@interface SomeClass (SomeBaseCategory)
    @property (nonatomic, retain) id somePrivateProperty;
@end

@implementation SomeClass {
    id _somePrivateProperty;
}

    - (void)setSomePrivateProperty:(id)property {

        _somePrivateProperty = property;
    }

    - (id)somePrivateProperty {
         return _somePrivateProperty;
    }

@end

Otherwise your app will crash.

In any case, keep in mind that given the dynamic nature of Objective-C, your property will never be fully private, since you can always send a message to an Objective-C object through objc_msgsend and thus set or read the property value.

EDIT:

If you do not have the source code for a class implementation, you cannot define a class extension (as per source linked above).

In this case, you could use object association to define properties .

Just add the category definition in the .m file OUTSIDE the implementation block

Like so:

@interface MyClass (MyCategory)
@property (assign) BOOL myPrivateProperty;
@end

@implementation MyClass
...
@end

Categories are best used for adding capability to code you do not own and cannot change. Adding properties via categories is not impossible, but is much more difficult.

Class Extensions are best used for keeping properties your object needs, but are not intended to be public.

If you do truly need to add properties to this object, the way to do it is with the Objective-C runtime's associated objects

There's an excellent writeup of when/how to use them here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM