加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 定义iOS类扩展中的属性

发布时间:2020-12-16 03:17:02 所属栏目:百科 来源:网络整理
导读:我想在类扩展中的UITableView中添加一个属性: @interface UITableViewController ()@property NSString *entityString;@end 然后我导入扩展名,然后在UITableViewController的子类中使用entityString属性: @implementation CustomerTableViewController- (v
我想在类扩展中的UITableView中添加一个属性:
@interface UITableViewController ()

@property NSString *entityString;

@end

然后我导入扩展名,然后在UITableViewController的子类中使用entityString属性:

@implementation CustomerTableViewController

- (void)viewDidLoad {
    self.entityString = @"Customer";
    ...
    [super viewDidLoad];
}
...

Apple documentation说:

the compiler will automatically synthesize the relevant accessor
methods (…) inside the primary class
implementation.

但是当我尝试执行它,我得到这个错误:

-[CustomerTableViewController setEntityString:]: unrecognized selector sent to instance 0x737b670

我究竟做错了什么?也许这个属性不能被子类访问?

解决方法

类扩展用于声明附加接口 – 方法和属性 – 其实现契约将在类的主要补充范围内得到满足.

这就是为什么你不能通过类扩展添加存储 – 添加ivars.类扩展是一个接口,不再多,不少. @synthesize是为@property声明创建存储,但@property的@synthesize只能出现在类的@implementation中(无论是显式还是作为编译器的默认行为).

由于您无法重新编译框架类,因此无法向其中添加ivars.

@ prashat的答案是将存储添加到现有类的一种方法.但是,这条路线通常是不合需要的;框架类的悬挂状态willy-nilly是设计不良的一个标志,并将使您的应用程序随着时间的推移更加难以维护.

更好地重新设计您的设计,了解为什么您目前需要将状态附加到不能直接包含它的对象,并重构该要求.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读