、仅 setDelegate 方法或同时使用两者之间有区别吗?

发布于 2024-08-22 11:16:56 字数 287 浏览 11 评论 0原文

我注意到,当我这样做时:

[myParser setDelegate:self];

它有效:D(即使我没有在头文件中添加代码......你知道,

<delegateStuff, delegateOtherStuff> 

在接口声明中)

你应该什么时候修改头文件以使你的委派工作? setDelegate 方法足以使其工作吗?

为任何帮助干杯;)

Gauthier

I noticed that when I do this :

[myParser setDelegate:self];

it works :D (even if I didn't add the code in the header file ... you know, the

<delegateStuff, delegateOtherStuff> 

in the interface declaration)

When are you supposed to modify the header file to make your delegate work ? Is the setDelegate method enough to make it work ?

Cheers for any help ;)

Gauthier

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

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

发布评论

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

评论(4

有深☉意 2024-08-29 11:16:56

简而言之,接口声明中的 不是必需的,它用于在编译时进行错误检查。如果您想接收委托消息,则需要调用 setDelegate: 方法

您引用的 是协议声明。 Objective-C 中的协议类似于 Java 中的接口:它定义了类响应的消息(方法)列表。然而,与 Java 不同的是,Objective-C 是动态类型的,这意味着任何消息都可以发送到任何对象,并且您只能确定它是否会在运行时响应它。

这样做的结果是,如果像 setDelegate: 这样的方法请求 id 类型的参数,您可以给它任何对象。 NSXMLParser 中的代码可以在发送之前检查它是否能够响应特定消息。这样你就可以实现你想要的任何委托方法。 (因为 Java 有更严格的类型检查,所以无论您是否需要全部方法,都必须实现接口中的所有方法。)

如果 setDelegate: 而是代表类型 id; 它现在说它想要一个实现 NSXMLParserDelegate 协议的对象。不过,这仅在编译时检查以帮助您捕获错误。如果您使用类型转换,您可以在其中发送任何对象,只要它响应所需的消息,您的程序就会正常运行。 (同样,在 Java 中,您可以使用类型转换来编译,但是如果对象的类型不正确,即使它具有相同的方法,类型转换本身也会抛出异常。)

顺便说一下,您是 子类化 NSXMLParser? (我认为是这样,因为您将 self 传递给 setDelegate:这不是它应该使用的方式! Cocoa 框架通常更喜欢委托而不是子类。您的委托类应该是一个扩展 NSObject 的单独类(或者如果您愿意的话可以是其他类)。

In short, the <NSXMLParserDelegate> in your interface declaration is not required, it's there for error checking at compile time. Calling the setDelegate: method is required if you want to receive delegate messages.

The <delegateStuff> you refer to are protocol declarations. A Protocol in Objective-C is like an Interface in Java: it defines a list of messages (methods) that the class responds to. Unlike Java, however, Objective-C is dynamically typed, meaning any message can be sent to any object and you can only be sure if it will respond to it at run time.

The upshot of this is that if a method like setDelegate: asks for a parameter of type id, you can give it any object whatsoever. The code in NSXMLParser can check if it is able to respond to a specific message before sending it. That way you can implement whatever delegate methods you want. (Because Java has more strict type checking, you must implement all the methods in an interface, whether you need them all or not.)

If setDelegate: instead act for a type id <NSXMLParserDelegate> it is now saying that it wants an object that implements the NSXMLParserDelegate protocol. This is only checked at compile time to help you catch errors, though. If you use a typecast you can send any object in there and, as long as it responds to the required messages, your program will run fine. (Again, in Java, you could use a typecast to compile, but the typecast itself would throw an exception if the object wasn't of the correct type, even if it had the same methods.)

By the way, are you subclassing NSXMLParser? (I assume so, since you are passing self to setDelegate:. That is not the way it is meant to be used! The Cocoa frameworks usually prefer delegates over subclasses. Your delegate class should be a separate class extending NSObject (or something else if you want).

毁梦 2024-08-29 11:16:56

当需要时,编译器会通过警告通知您。

我注意到,当您将 添加到头文件时,Xcode 能够自动完成您可能想要使用的任何委托方法。我每次都这么做就是为了这个原因。

When it's required, the compiler will let you know with a warning.

I've noticed that when you add the to the header file, Xcode is able to autocomplete any delegate methods you might want to use. I do it every time just for this reason.

Saygoodbye 2024-08-29 11:16:56

iPhone SDK 4.0 下如果缺少,编译器会发出警告

The compiler now warns if is missing under iPhone SDK 4.0

微凉 2024-08-29 11:16:56

只要“委托”实现了在运行时调用的选择器,它就始终可以工作。声明匹配的委托协议不是必需的,但如果委托键入的内容不仅仅是“id”(例如,id [SomeDelegateProtocol] 委托),您可能会收到编译器警告。

但是......如果您声明一个类满足特定协议的规范,那么(a)该协议必须存在并且(b)您的类将被检查协议选择器。 iPhone SDK 4 中现在包含的 NSXMLParserDelegate 将导致旧应用程序发出警告;如果您添加协议,然后尝试向后编译到 3.x,您将收到编译错误,因为该协议在 4 之前不存在(至少您不太可能包含具有该协议的框架)。我刚开始将当前应用程序从 3 迁移到 4 时遇到了这种情况。我讨厌无法轻易消除的警告。

A "delegate" will always work as long as it implements the selectors that are called at runtime. Declaring a matching delegate protocol is not required, but you can get a compiler warning if the delegate is typed to more than just "id" (e.g., id [SomeDelegateProtocol] delegate).

However... if you declare that a class meets the spec of a particular protocol, then (a) that protocol must exist and (b) your class will be checked for the protocols selectors. The NSXMLParserDelegate that is now included with iPhone SDK 4 will cause older apps to throw a warning; if you add the protocol and then try to backward compile to 3.x you will get a compile error because the protocol does not exist prior to 4 (at least you are unlikely to have included a framework with that protocol in it). I just encountered this as I'm beginning to migrate current apps from 3 to 4. I hate warnings that I cannot easily eliminate.

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