检查一个对象是否符合 Objective-C 中的两个单独的协议

发布于 2024-10-11 05:21:22 字数 479 浏览 2 评论 0原文

在 Objective-C 中,当你声明一个实例变量时,你可以在编译时检查它是否符合分配协议,如下所示:

id <MyProtocol> variable;

Is it possible to check if a object allocates to the variableconsole to meet multiple differentprotocol atcompile time?如:

id <MyProtocol, MyOtherProtocol> variable;

我知道我可以使用 conformsToProtocol:respondsToSelector 等进行运行时检查(我在实际使用该对象之前执行此操作以提高安全性),并且我可以编写我自己的 setter 方法可以进行检查,但我想在编译时知道。

In Objective-C when you declare an instance variable you can check if it conforms to a protocol on assignment at compile time like so:

id <MyProtocol> variable;

Is it possible to check whether an object assigned to the variable conforms to two separate protocols at compile time? As in:

id <MyProtocol, MyOtherProtocol> variable;

I know I can do runtime checking using conformsToProtocol: and respondsToSelector et al, (which I do before actually using the object for added safety), and I could write my own setter method that does the check, but I'd like to know at compile time.

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

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

发布评论

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

评论(3

何必那么矫情 2024-10-18 05:21:23

是的,该语法是正确的。

检查对象是否符合协议的正确方法是这样做:

if ([myObj conformsToProtocol:@protocol(MyProtocol)]) {
  //conformance!
}

请注意,这既可以用作实例方法,也可以用作类方法。

如果由于某种奇怪的原因您无法使用 conformsToProtocol:,您可以下降到运行时级别:

#import <objc/runtime.h>

Protocol * p = objc_getProtocol("MyProtocol");
if (class_conformsToProtocol([myObj class], p)) {
  //conformance!
}

Yes, that syntax is correct.

The correct way to check if an object conforms to a protocol is to do this:

if ([myObj conformsToProtocol:@protocol(MyProtocol)]) {
  //conformance!
}

Note that this works as both an instance method and a class method.

If for some bizarre reason you can't use the conformsToProtocol:, you can drop down to the runtime level:

#import <objc/runtime.h>

Protocol * p = objc_getProtocol("MyProtocol");
if (class_conformsToProtocol([myObj class], p)) {
  //conformance!
}
东风软 2024-10-18 05:21:23

我认为最好是使用自己的代码:

id <MyProtocol, MyOtherProtocol> variable;

并且在调用方法之前,检查变量是否响应您想要调用的内容:

if ([variable respondsToSelector:@selector(aMethod:)]) {
    [variable aMethod:nil];
}

由于 Objective-C 是动态语言,仅声明变量协议并不能保证它符合到协议。当你构建时它主要会生成警告。

I think the best is to use your own code:

id <MyProtocol, MyOtherProtocol> variable;

And before calling a method, check if the variable responds to what you want to call:

if ([variable respondsToSelector:@selector(aMethod:)]) {
    [variable aMethod:nil];
}

Since Objective-C is a dynamic language, just declaring the variable protocol can't assure that it conforms to the protocol. It'll mostly generates warnings when you build.

Spring初心 2024-10-18 05:21:23

您不能总是在编译时检查协议一致性,因为从编译器的角度来看,(非限定)类型 id 的对象始终是有效对象。例如,

id<P1> p1;
id<P2> p2;
id p0;

// compiler warning: assigning to 'id<P1>' from incompatible type 'id<P2>'
p1 = p2;

// no compiler warning
p0 = p2;
p1 = p0;

话虽这么说,如果对象不符合编译时已知的两个协议, 将会向您发出警告 -时间:

id<P1> p1;
id<P2> p2;
id<P1, P2> p12;
id<P1, P2> q12;
id p0;

p12 = p1; // warning: assigning to 'id<P1,P2>' from incompatible type 'id<P1>'
p12 = p2; // warning: assigning to 'id<P1,P2>' from incompatible type 'id<P2>'
p12 = q12; // no compiler warning

// no compiler warning
p0 = p1;
p12 = p0;

You can’t always check protocol conformance in compile-time because an object of (non-qualified) type id is always a valid object from the compiler’s perspective. For instance,

id<P1> p1;
id<P2> p2;
id p0;

// compiler warning: assigning to 'id<P1>' from incompatible type 'id<P2>'
p1 = p2;

// no compiler warning
p0 = p2;
p1 = p0;

That being said, <P1, P2> will give you warnings in case the object doesn’t conform to both protocols if that can be known at compile-time:

id<P1> p1;
id<P2> p2;
id<P1, P2> p12;
id<P1, P2> q12;
id p0;

p12 = p1; // warning: assigning to 'id<P1,P2>' from incompatible type 'id<P1>'
p12 = p2; // warning: assigning to 'id<P1,P2>' from incompatible type 'id<P2>'
p12 = q12; // no compiler warning

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