简体   繁体   English

objective-C如何声明私有财产的类别?

[英]objective-C how to declare private property for category?

I'm new to objective-C, so apologies if this is repeated somewhere. 我是Objective-C的新手,如果在某个地方重复这一点,请道歉。 I have a category(?) that is something like: 我有一个类别(?),如:

inside SomeClass.h : 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: 现在在我的SomeClass.m ,我只有:

@implementation SomeClass (SomeCategory)

// dynamic setters/getters here for someProperty.

@end

I think the someProperty is public. 我认为someProperty是公开的。 how do i make this "private"? 我如何让这个“私人”? (in other words, how do i syntactically put this in the .m file? i tried to use (换句话说,我如何在语法中将它放在.m文件中?我试图使用

@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. .h文件中,您不应该提供类别。 Just use: 只需使用:

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

In your .m file, define your private property inside a class extension : .m文件中,在类扩展中定义私有属性:

@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. 在任何情况下,请记住,鉴于Objective-C的动态特性,您的属性永远不会完全私有,因为您始终可以通过objc_msgsend向Objective-C对象发送消息,从而设置或读取属性值。

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 只需在实现块外的.m文件中添加类别定义即可

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 如果确实需要向此对象添加属性,那么执行此操作的方法是使用Objective-C运行时的关联对象

There's an excellent writeup of when/how to use them here 还有的时候/如何使用它们的优秀的书面记录在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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