2017-01-18 26 views
0

我知道我可以調用一個類的方法一樣[MyClass myMethod];[NSClassFromString(@"MyClass") performSelector:@selector(myMethod)];iOS的Objective-C的 - 調用類方法在類動態創建了NSClassFromString

我目前正試圖但是要做到這一點:

[MyClass configureWithNum:@"12345" 
     andNumber:@"2312312" 
     options:nil 
     completion:^(NSArray<SecondClass*>* array) {} 
    ]; 

當我這樣做:

[NSClassFromString(@"MyClass") performSelector:@selector(configureWithNum) 
      withObject:@"12345" 
      withObject:@"2312312" 
      withObject:nil 
      withObject:^(NSArray<SecondClass*>* array) {}]; 

它拋出一個Undeclared selection 'configureWithNum'

看起來好像performSelector withObject調用只適用於一個參數。所以我懷疑我需要採用this這樣的做法,使用objc_msgSend,但我似乎無法獲得正確的格式。

我覺得this SO是沿着正確的軌道 - 但我似乎無法得到它的權利。當我這樣做:

objc_msgSend(NSClassFromString(@"MyClass"), sel_getUid("configureWithNum:andNumber:options:completion:"), @"12345", @"2312312", nil, ^(NSArray<AdColonyZone*>* zones) {}); 

它給我一個implicit declaration of function 'objc_msgSend' is invalid in C99錯誤。

任何想法如何使用無論是objc_msgSendperformSelector: withObject:實現這個電話嗎?

回答

0

因爲沒有多個withObject的方法。您應該將所有數據封裝在單個對象中,例如NSArray或NSDictionary,然後將其作爲參數傳遞。

爲例子:

NSArray * arrayParams = 
[NSArray arrayWithObjects: @"firstParams", @"secondParams", nil]; 

[self performSelector:@selector(configureWithNum:) 
      withObject:arrayParams]; 
+0

所以對於這一個,我不需要明確地調出參數名稱,我可以只是通過參數或多或少? –

0

Implicit declaration of function 'objc_msgSend' is invalid in C99錯誤意味着,你不包括需要的頭。

#include <objc/message.h> 

應該解決這個問題。