Objective-C 运行时中类似 IBOutlet 的构造

发布于 2024-07-29 16:42:36 字数 262 浏览 4 评论 0原文

我对 IBOutlets 的理解是它们充当 Objective-C 类中 ivars 和属性的标记。 Objective-C 运行时中是否有任何东西允许人们在运行时查询 ivar 或属性或类是否已用 IBOutlet 标记? 或者 XCode 只是在编译时对这些做了一些巧妙的事情?

如果它们是运行时构造,是否可以定义自己的标记并以这种方式使用它们:

@private
    MyMarker MyClass instance;

My understanding of IBOutlets is that they act as a marker to ivars and properties in Objective-C classes. Is there anything in the Objective-C runtime that would allow one to query whether an ivar or property or a class has been marked with an IBOutlet at runtime? Or does XCode just do something clever with these at compile time?

If they are a runtime construct, is it possible to define ones own markers and use them in this way:

@private
    MyMarker MyClass instance;

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

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

发布评论

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

评论(2

痴情 2024-08-05 16:42:37

据我了解,Interface Builder 只是读取头文件。 IBOutlet 和 IBAction 是微不足道的#defines:

#define IBOutlet
#define IBAction void

根本不影响编译。 Interface Builder 直接读取您的头文件(当头文件发生变化,但它只是读取并解析头文件本身。

当 nib 文件被解档时,通过普通接口使用 ivar 或属性设置值,但没有什么特别之处,即 ivar/属性可由接口使用。 所以不

,IBOutlet/IBAction 属性的存在不会被存储并且无法访问,您也不能添加自己的属性,

您可以查看属性并查看是否有任何有用的内容可以附加到 ivar 。属性,但我会感到非常惊讶。

It is my understanding that Interface Builder simply reads the header files. IBOutlet and IBAction are trivial #defines:

#define IBOutlet
#define IBAction void

that do not affect the compile at all. Interface Builder reads your header files directly (it is notified by Xcode when the header files change, but it just reads and parses the header files itself.

When the nib files are dearchived, the values are set using the ivar or property through the normal interface, but nothing special is noted that the ivar/property is usable by Interface Builder.

So no, the existence of the IBOutlet/IBAction property is not stored and cannot be accessed, nor can you add your own properties.

You could look at attributes and see if there is anything useful that could be attached to the ivar with an attribute, but I'd be very surprised.

风启觞 2024-08-05 16:42:37

是的,IBOutlet 和 IBAction 只是在预编译阶段被解析器丢弃,因此编译后的输出中没有任何内容。 如上所述,它们只是由 Interface Builder 以文本方式处理,以便它知道哪些属性/方法子集可供连接窗口使用。

然而,这并不能阻止你自己做同样的事情——你可以定义一些由预处理器编译掉的#define,并使用文本处理来操纵它们。 但这些在运行时都不可用,这意味着您无法真正执行您的建议。

从技术上讲,可以编写一个宏来对属性/ivar 进行一些操作,然后向不同的 ivar 添加额外的信息; 例如:

#define OUTLET(type,name) type name;BOOL property_##name;
@interface Foo : NSObject
{
        OUTLET(NSString*,foo);
}
@end

将扩展为

@interface Foo :NSObject
{
    NSString* foo;
    BOOL property_foo;
}
@end

,然后您可以使用 property_foo 的存在来对您的代码执行某些操作(这应该在运行时和编译时都可以检测到)。

不过,我不建议尝试这样做……首先,它会使您的界面(以及内存对象)比它们需要的更大。 您最好创建自己的类(或 struct typedef)来保存您想要的附加信息。

Yes, IBOutlet and IBAction are just thrown away by the parser at the precompilation stage, so there's nothing in the compiled output. And as noted above, they're just textually processed by Interface Builder so that it knows what subset of properties/methods to make available to the connections window.

However, that doesn't stop you doing the same thing yourself - you could just define some #define that are compiled away by the preprocessor, and using textual processing manipulate them. But none of these are available at runtime, which means that you can't really do what you propose.

It's technically possible to write a macro that would do some manipulation of a property/ivar and then add extra information to a different ivar; for example:

#define OUTLET(type,name) type name;BOOL property_##name;
@interface Foo : NSObject
{
        OUTLET(NSString*,foo);
}
@end

would expand to

@interface Foo :NSObject
{
    NSString* foo;
    BOOL property_foo;
}
@end

and you could then use the existence of property_foo to do something with your code (which should be detectable at runtime as well as compile time).

I wouldn't recommend trying to do this generally though ... for a start, it will make your interface (and therefore memory objects) larger than they'd need to be. You'd be better off creating your own class (or struct typedef) to hold the additional information you want.

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