为什么我不能@@ synthesize分类器中的访问器? [英] Why can't I @synthesize accessors in a category?

查看:93
本文介绍了为什么我不能@@ synthesize分类器中的访问器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Obj-C 2.0让我在一个类别中声明属性,但是编译器拒绝在该类别内合成访问器.为什么?

Obj-C 2.0 lets me declare properties in a category, but the compiler refuses to synthesize accessors inside the category. Why?

(有时,将一堆相关的东西放在一个类别中对组织而言是有意义的-即使支持iVars进入类声明.我也知道类扩展和如何做私有属性,但这不是我的用处情况.)

(Sometimes it makes organization sense to put a bunch of related stuff in a category - even if the backing iVars go in the class declaration. I know about class extensions and how to do private properties, but that isn't my use case.)

推荐答案

问题是类别在逻辑上与其类分开,甚至分别存储在二进制文件中.内部实现是类描述结构包含一个方法列表数组,该方法列表最初仅包含在主@implementation块中定义的方法列表.随着ObjC链接器模块加载新类别,它们的方法列表将添加到该数组中.

The issue is that categories are logically separate from their classes, and are even stored separately in the binary file. The internal implementation is that a class description structure holds an array of method lists which initially contains only the list of methods defined in the main @implementation block. As the ObjC linker modules load new categories, their method lists are added to that array.

由于此实现,类别本身无法访问类的存储,因此无法对其进行修改(这也提出了在卸载类别时该怎么做的问题).

Because of this implementation, the categories themselves have no means of accessing the storage for a class, and therefore cannot modify it (it also opens up the question of what to do when the category is un-loaded).

最后,从更逻辑而非技术的角度来看,这种想法是,类别不具有类的内存结构的任何所有权",它只是关联了一些新方法.要完全支持属性综合,就需要以某种方式修改类的存储空间

Lastly, from a more logical rather than technical perspective, the idea is that a category doesn't have any 'ownership' of the in-memory structure of a class, it simply associates some new methods. To fully support property synthesis, it would need to modify the class's storage in some way

解决方案?您可以将@synthesize语句放在主要的@implementation块中,或者直接在类别@implementation中实现自己的访问器.

The solution? You either put the @synthesize statements inside your main @implementation block, or you just implement your own accessors directly within the category @implementation.

这篇关于为什么我不能@@ synthesize分类器中的访问器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆