English 中文(简体)
你们如何知道在目标C中是否采用超级方法
原标题:How do you know whether to invoke a superclass method in Objective C

班级的儿童为家长。 父母执行C议定书,其中规定了任择方法,包括<代码>-(撤销)d。 儿童是否实施了<条码>-d;它是否应当援引<条码>。

换言之,如果并且只有某种情况会对其作出反应,那么我写了什么法典来援引[super d]? 假定我不控制父母的履行;它可能随时改变。

Here are all the ways I have thought of. I am currently using number 4.

www.un.org/Depts/DGACM/index_spanish.htm 明显可见的答复1:

[super d]; // Delete this line if a runtime exception occurs when you try it

This does not work because Parent might implement -d dynamically so this works when you test it and not in the field. Or the implementation of Parent could change so that the result of this test is no longer correct.

www.un.org/Depts/DGACM/index_spanish.htm 明显可见的答复2:

if ([super respondsToSelector:_cmd])
    [super d];

这并不奏效,因为国家统计局的目标在于执行——答复。 ToSelector将发现儿童的执行情况,并在一切情况下将YES送回。

www.un.org/Depts/DGACM/index_spanish.htm 明显可见的答复:

if ([[self superclass] instancesRespondToSelector:_cmd])
    [super d];

只有当超级阶级知道它总是执行——d 时,才进行这项工作;如果能够动态地确定这一方法是否存在的话,这种技术就不会奏效。 更不用说,它将在管理时间对父母的执行进行静态改变。

Apparently sensible answer 4:

@try
{
    [super d];
}
@catch (NSException *exception)
{
    NSString *templateReason = [NSString stringWithFormat:
                                @"-[%@ %@]: unrecognized selector sent to instance %p"
                                ,NSStringFromClass([self superclass])
                                ,NSStringFromSelector(_cmd)
                                ,self];
    if (![exception.reason isEqualToString:templateReason])
        @throw exception;
}

如果不存在超级舱内的方法,这种性能就会很差,因为计算模板Reason,然后将其与例外理由进行比较,费用昂贵。

这一机制很脆弱,因为本案中说明的例外理由的形式可能会在未来的SDK或临时释放中得到改变。

最佳回答

这些事情都不必要。

如果你将某些类别或其它类别分开,你就已经需要知道你是否正在取代或补充行为。

换言之,如果执行存在,而且你希望执行的方式不同,你不会说是多余的。

如果执行没有,你不会说过。

如果实施does,但你希望加以补充,请上下台。

<><>>

Wether the implementation can change at any time isn t relevant for your question; what is important is if the interface changes.

如果interface 不断变动,奇迹好,那类人就是一个非常贫穷的候选人,可以分级或甚至使用。

问题回答

不幸的是,我不是这些答案是否好。 不幸的是,这样做是为了达到目的,甚至可能并非要由你来称超级舱法,有时凌驾于一种方法,就是要替换这种方法,而不是把你的功能放在超级舱功能之上。

接下来是阅读文件,选择正确的方法。

If this is about a framework that you are implementing and wish to use a consistent approach, then 2 or 3 should be fine. 1 and 4 rely on exceptions - which are not really intended to be used for anything except truly exceptional issues in objective-c.

In objective c you can define methods in protocols to be required or optional, but your never sure if a class that is conform to a protocol, actually implements that protocol.

So you always have to check if the instance responds to a protocol.
I would choose for option 2, this is the most elegant. When you will make the protocol method optional in the future, this will still be the correct solution.

Option 4 I personally find to much Java-ish.





相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签