2012-01-02 27 views

回答

7

我不是一個Ruby程序員,所以我可能會丟失一些微妙,但似乎andand使得它讓您可以安全鏈的方法調用,即使一個方法調用可能返回零。這是在Objective-C中構建的,因爲調用方法(實際上正確地稱爲發送消息)爲零是安全的並且完全可以接受的。消息總是返回零,基本上沒有任何操作。在Objective-C,這是好的:

id foo = [someObject objectByDoingSomething]; // foo might be nil 

id bar = [foo objectByDoingSomethingElse]; 

// If foo is nil, calling objectByDoingSomethingElse is essentially 
// a no-op and returns nil, so bar will be nil 
NSLog(@"foo: %@ bar: %@", foo, bar); 

因此,這也沒關係,即使objectByDoingSomething可能返回nil:

id foo = [[someObject objectByDoingSomething] objectByDoingSomethingElse]; 

The Objective-C Programming Language:「在Objective-C,它是有效的發一個消息到零 - 它在運行時不起作用。「該文檔包含有關Objective-C中nil上調用方法行爲的更多詳細信息。