在 Objective-C 中,在类扩展中声明新属性是一种不好的做法吗?

发布于 2024-11-01 04:18:47 字数 1047 浏览 1 评论 0原文

类扩展的一个强大优点是,通过类扩展,您可以在头文件中声明只读属性,并在类扩展中将此属性重写为读写属性。如下所示:

//SomeClass.h

@interface SomeClass : NSObject
{
    NSInteger someInt;   //with modern runtime you can omit this line
}
@property (readonly) NSInteger someInt;
@end

//SomeClass.m
@interface SomeClass ()
@property (readwrite) NSInteger someInt;
@end

@implementation SomeClass
@synthesize someInt;
@end

但是如果您使用现代运行时,您还可以在类扩展中声明一个全新的属性(如果没有,它也会为该属性生成一个 iVar)。

//SomeClass.h

@interface SomeClass : NSObject
{
}
@end

//SomeClass.m
@interface SomeClass ()
@property (readwrite) NSInteger someInt;
@end

@implementation SomeClass
@synthesize someInt;
@end

这是我的问题:我认为在类扩展中声明一个全新的属性在某种程度上会产生一些副作用。因为类扩展可能不在头文件中,并且对该类进行子类化的其他人可能不知道该“秘密属性”。而如果他申报了与那个“秘密财产”同名的财产。这个新属性的 getter 和 setter 方法将覆盖超类的。这不是一个问题吗?为什么现代运行时会允许这样的事情发生?

编辑我发布了有关此主题的另一个问题,请查看: 风险在类扩展(Ojbective-C)中声明新属性,如何解决?

One strong advantage of class extension is that with class extension you can declare a readonly property in the header file and override this property in class extension as readwrite property. Like below :

//SomeClass.h

@interface SomeClass : NSObject
{
    NSInteger someInt;   //with modern runtime you can omit this line
}
@property (readonly) NSInteger someInt;
@end

//SomeClass.m
@interface SomeClass ()
@property (readwrite) NSInteger someInt;
@end

@implementation SomeClass
@synthesize someInt;
@end

But if you use a modern runtime ,you can also declare a totally new property in the class extension (which also generate an iVar for that property if there isn't).

//SomeClass.h

@interface SomeClass : NSObject
{
}
@end

//SomeClass.m
@interface SomeClass ()
@property (readwrite) NSInteger someInt;
@end

@implementation SomeClass
@synthesize someInt;
@end

Here's my question : I think declare a totally new property in class extention is somehow has some side effects. Because class extension my not be in the header file and someone else who subclass the class may not know about that "secret property". And if he declare a property with the same name of that "secret property". And this new property's getter and setter method will override the super class's. Isn't this a problem?And why would modern runtime allow such thing happen?

EDIT I posted another question about this topic , please check it out:
The risk of declare new propery in class extension (Ojbective-C) , how to solve it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

屌丝范 2024-11-08 04:18:47

我不认为在类扩展中声明新属性是不好的做法。我以一定的频率这样做。首先在标头中包含 readonly 属性的唯一原因是允许其他类获取该值,同时只允许您修改它。很多时候,ivar 应该与其他类无关,而只是一个实现细节。因此,它在头文件中没有位置。

将此 ivar 实现为私有属性(仅在类扩展中声明的新属性)仍然有用,因为它可以为您抽象方便的内存管理样板代码。不幸的是,名称冲突只是 Objective C 中的一个事实。Apple 列出了一些非常明确的 命名约定供您遵循(或遵循),以防止与他们的方法名称。如果您担心与使用该私有属性无形地创建的 getter 和 setter 发生冲突,只需采用并严格遵循您在实现私有属性时使用的那些私有属性名称的命名约定即可。私人财产。这是使用 Objective C 所能做的最好的事情,但我个人认为好处大于风险。

I don't think it's bad practice to declare a new property in a class extension. I do this with some frequency. The only reason to include the readonly property in the header in the first place is to allow other classes to get the value, while only you are allowed to modify it. Quite often, that ivar should be of no concern to other classes, and is an implementation detail only. As such, it has no place in the header file.

Implementing this ivar as a private property (a new property only declared in the Class Extension) is still useful, because of the convenient memory management boilerplate code it can abstract for you. Unfortunately, name collisions are just a fact of life in Objective C. Apple lays out some pretty clear naming conventions for you to follow (or not follow) to prevent collisions with their method names. If you're worried about collisions with the getters and setters you've invisibly created with that private property, just adopt and obsessively follow some naming convention for those private property names that you only ever use when implementing a private property. That's the best you're going to do with Objective C, but I personally think the benefits outweigh the risks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文