使用“部署目标”重命名方法和 Cocoa Touch 中的弱链接
在 iPhone SDK 3.2 中,[CLLocation getDistanceFrom:]
已重命名为 [CLLocation distanceFromLocation:]
。我的项目是使用 3.2 作为基础 SDK 和 3.0 的“部署目标”进行编译的,这意味着仅在 3.2 中可用的框架是弱链接的,因此我的应用程序仍然可以在 3.0 上运行。
如果我将呼叫更改为使用新名称 distanceFromLocation:
,运行 3.0 或 3.1 的设备上会发生什么情况?我假设该方法调用将失败并且将返回0.0
(因为Cocoa返回无法识别的选择器的默认值)。
如果我的假设是正确的,这意味着我现在必须忍受编译器警告,直到我不再想以 3.0 为目标。
In the iPhone SDK 3.2, [CLLocation getDistanceFrom:]
was renamed to [CLLocation distanceFromLocation:]
. My project is compiled with 3.2 as the Base SDK and a "Deployment Target" of 3.0, meaning frameworks only available in 3.2 are weak linked so my app can still run on 3.0.
If I change my calls to use the new name distanceFromLocation:
, what will happen on devices running 3.0 or 3.1? I assume that the method call will fail and 0.0
will be returned (as Cocoa returns the default for unrecognized selectors).
If my assumption is correct this means I have to live with the compiler warnings for now, until I no longer want to target 3.0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,您正在考虑向
nil
发送消息。向无法识别选择器的对象发送消息将导致异常。如果您没有捕获该异常,您的应用程序将会崩溃。用户不会喜欢这样,苹果可能会因此拒绝你的应用程序。解决方案是测试该位置是否响应新的选择器:
顺便说一句,这实际上与链接没有任何关系。如果我们谈论的是一个已更改名称的类,那么它就会更改名称,但动态消息调度意味着 Objective-C“方法调用”(更准确地说,是到对象的消息)在编译或链接时不绑定。
No, you're thinking of sending messages to
nil
. Sending a message to an object that doesn't recognize the selector will cause an exception. If you don't catch that exception, your application will crash. Users will not like this, and Apple may reject your app over it.The solution is to test whether the location responds to the new selector:
Incidentally, this really doesn't have anything to do with linking. If we were talking about a class that had changed names, then it would be, but dynamic message dispatch means Objective-C “method calls” (more precisely, messages to objects) are not bound at compile or link time.