重写方法时的严格类型与宽松类型

发布于 2024-09-28 04:37:45 字数 829 浏览 6 评论 0原文

我有一个名为 AddressCard 的类,来自“Objective C 编程”中的示例,并且我正在实现 isEqual: 方法。

NSObject 中此方法的签名对参数使用松散类型:

- (BOOL)isEqual:(id)anObject

OTOH,书中的示例代码使用严格类型:

- (BOOL) isEqual:(AddressCard *) aCard

我不确定我是否完全理解编译器在这种情况下所做的事情。我尝试将 AddressCard 与 NSString ([aCard isEqual: @"Foo"]) 进行比较,期望出现运行时错误(如果系统使用我的方法)或者系统将调用 NSObject 的 IsEqual 版本。

相反,当我的 IsEqual: 尝试调用特定于 AddressCard: 的方法时,我的方法被调用(即使参数是 NSString 而不是 AddressCard)并引发异常:

- (BOOL) isEqual:(AddressCard *) aCard {
    if ([name isEqualToString: [aCard name]] && /*here I get the error*/
        [email isEqualToString:[aCard email]]) {
        return YES;
    }else {
        return NO;
    }
}

发生了什么事? NSString 究竟是如何传递给需要其他内容的方法的?重写方法时更改方法的签名是否可以?

I have a class called AddressCard from an example in "Programming in Objective C", and I'm implementing a isEqual: method.

The signature of this method in NSObject uses loose typing for the parameter:

- (BOOL)isEqual:(id)anObject

OTOH, the sample code in the book uses strict typing:

- (BOOL) isEqual:(AddressCard *) aCard

I'm not sure I fully understand what the compiler does in this case. I tried comparing an AddressCard to a NSString ([aCard isEqual: @"Foo"]) expecting either a runtime error (if the system uses my method) or that the system would call NSObject's version of IsEqual.

Instead, my method was called (even though the parameter was a NSString and not an AddressCard) and raised an exception when my IsEqual: tried to call a a method specific to AddressCard:

- (BOOL) isEqual:(AddressCard *) aCard {
    if ([name isEqualToString: [aCard name]] && /*here I get the error*/
        [email isEqualToString:[aCard email]]) {
        return YES;
    }else {
        return NO;
    }
}

What's going on? How on Earth is an NSString being passed to a method that expects something else? Is changing the signature of a method OK when overriding it?

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

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

发布评论

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

评论(2

年华零落成诗 2024-10-05 04:37:45

运行时通过消息的选择器来区分消息。所有具有相同名称的方法都具有相同的选择器。方法参数对选择器没有影响。在您的情况下,选择器是 isEqual:

这是来自 Apple 的“The Objective-C 编程语言”(强调我的):

消息传递例程只能通过选择器访问方法实现,因此它会同等对待具有相同选择器的所有方法。它从选择器中发现方法的返回类型及其参数的数据类型。 因此,除了发送到静态类型接收器的消息之外,动态绑定要求所有同名方法的实现具有相同的返回类型和相同的参数类型。(静态类型接收器是此规则的一个例外,因为编译器可以从类类型中了解方法的实现。)

换句话说:更改现有方法的签名不是好的形式(IMO),但只要您静态键入这些方法的接收者(在您的情况下,这意味着 aCard 必须声明为 AddressCard *)。对于运行时来说,这没有问题。

不幸的是,您没有提及编译器是否会向您发出警告,因为您在需要 AddressCard * 的地方传递了 NSString * 。我希望它会这样做。

The runtime distinguishes messages by their selector. All methods with the same name have the same selector. Method arguments have no influence on the selector. In your case, the selector is isEqual:.

This is from Apple's "The Objective-C Programming Language" (emphasis mine):

The messaging routine has access to method implementations only through selectors, so it treats all methods with the same selector alike. It discovers the return type of a method, and the data types of its arguments, from the selector. Therefore, except for messages sent to statically typed receivers, dynamic binding requires all implementations of identically named methods to have the same return type and the same argument types. (Statically typed receivers are an exception to this rule, since the compiler can learn about the method implementation from the class type.)

In other words: changing the signature of an existing method is not good form (IMO) but it's fine as long as you statically type the receivers of these methods (in your case, that means aCard must be declared as AddressCard *). For the runtime, this is no problem.

Unfortunately, you are not mentioning whether the compiler gives you a warning because you are passing an NSString * where it expects an AddressCard *. I would expect it to do so.

新人笑 2024-10-05 04:37:45

我最好的猜测:编译器看到的只是一个期望使用指针参数调用指针的方法。编译器没有问题。

My best guess: All the compiler sees is a method that expects a pointer gets called with a pointer parameter. No problems for the compiler.

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