5

有沒有辦法找出 - 在運行時 - 給定的方法是否是可變類型的?像method_getTypeEncoding();那不會告訴我一個方法是否接受可變數量的參數。或者有這樣的訣竅可以說明嗎?在運行時確定Objective-C方法是否可變

+1

你能解釋一下你想要這些信息的背景嗎?我不確定在運行時如何發現它,但我也從不需要構建大量可變參數方法。基本上我得到的可能是你不需要知道,而且還有一個更大的問題。也許,但我總是可以錯了:) – 2012-07-16 20:00:30

+0

當你在一個veridic方法上調用'method_getNumberOfArguments()'會發生什麼?我從來沒有嘗試過,但也許它會返回一個特殊的值(如-1或其他)?另見'method_copyArgumentType()'。 – user1118321 2012-07-16 22:23:08

+1

@ user1118321'method_getNumberOfArguments'返回最小計數,就像它是普通方法一樣。 – 2012-07-16 22:31:00

回答

6

Robert的評論是正確的。試想一下:

@interface Boogity 
@end 
@implementation Boogity 
- (void)methodWithOneIntArg:(int)a {;} 
- (void)variadicMethodWithIDSentinel:(id)a, ... {;} 
@end 

上生成的二進制運行strings生產(也有股票main()):

strings asdfasdfasdf 
Boogity 
methodWithOneIntArg: 
variadicMethodWithIDSentinel: 
[email protected]:8i16 
[email protected]:[email protected] 
Hello, World! 

如果我改變可變參數的方法聲明爲- (void)variadicMethodWithIDSentinel:(int)a, ...,該strings輸出變爲:

Boogity 
methodWithOneIntArg: 
variadicMethodWithIDSentinel: 
[email protected]:8i16 
Hello, World! 

所以,不,沒辦法告訴。

相關問題