在Objective C / iOS中相当于Ruby method_missing [英] Equivalent of Ruby method_missing in Objective C / iOS

查看:100
本文介绍了在Objective C / iOS中相当于Ruby method_missing的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,Objective C / iOS是非常动态的语言,它是否像Ruby一样动态?它是否相当于method_missing?我问同样的问题js Javascript是否有像Ruby的method_missing这样的东西功能?并且对js不支持它感到失望。

As far as I understand Objective C / iOS is very dynamic language, so is it as dynamic as Ruby ? Does it have the equivalent of method_missing ? I asked the same question for js Does Javascript have something like Ruby's method_missing feature? and was disappointed that js does not support it yet.

推荐答案

Objective-C是动态的,尽管有我是一名Ruby程序员,我认为它不像Ruby那么动态。

Objective-C is dynamic, although having been a Ruby programmer, I would say it is not quite as dynamic as Ruby.

Objective-C确实有一个等效的method_missing。您将要覆盖 forwardInvocation: methodSignatureForSelector:,并遵循Apple的这一重要建议:

Objective-C does have an equivalent of method_missing. You'll want to override both forwardInvocation: and methodSignatureForSelector: and follow this important advice from Apple:


重要要响应对象本身无法识别的方法,除了forwardInvocation:之外,还必须覆盖methodSignatureForSelector :.转发消息的机制使用从methodSignatureForSelector获取的信息来创建要转发的NSInvocation对象。您的重写方法必须为给定的选择器提供适当的方法签名,方法是预先制定一个或通过询问另一个对象。

Important To respond to methods that your object does not itself recognize, you must override methodSignatureForSelector: in addition to forwardInvocation:. The mechanism for forwarding messages uses information obtained from methodSignatureForSelector: to create the NSInvocation object to be forwarded. Your overriding method must provide an appropriate method signature for the given selector, either by preformulating one or by asking another object for one.

Don不要使用 doesNotRecognizeSelector:,因为Apple警告它必须始终导致抛出异常。

Don't use doesNotRecognizeSelector: as Apple warns that it must always result in an exception being thrown.

有关详细信息,请参阅NSObject类文档: http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html

Please see the NSObject class documentation for further details: http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html

这篇关于在Objective C / iOS中相当于Ruby method_missing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆