有沒有辦法調用[anObject performSelector];超過2個對象?我知道你可以使用一個數組來傳遞多個參數,但我想知道是否有一個較低級別的方法來調用一個函數,我已經定義了更多的2個參數,而沒有使用具有參數nsarray的輔助函數。performSelector with more than 2 objects
16
A
回答
48
或者(1)使用NSInvocation或(2)直接使用objc_msgSend
。
objc_msgSend(target, @selector(action:::), arg1, arg2, arg3);
(注:確保所有的參數都是id
的,否則參數可能未正確發送。)
14
可以擴展NSObject
類是這樣的:
- (id) performSelector: (SEL) selector withObject: (id) p1
withObject: (id) p2 withObject: (id) p3
{
NSMethodSignature *sig = [self methodSignatureForSelector:selector];
if (!sig)
return nil;
NSInvocation* invo = [NSInvocation invocationWithMethodSignature:sig];
[invo setTarget:self];
[invo setSelector:selector];
[invo setArgument:&p1 atIndex:2];
[invo setArgument:&p2 atIndex:3];
[invo setArgument:&p3 atIndex:4];
[invo invoke];
if (sig.methodReturnLength) {
id anObject;
[invo getReturnValue:&anObject];
return anObject;
}
return nil;
}
(請參閱Three20項目中的NSObjectAdditions。)然後,您甚至可以擴展上述方法以使用varargs和以nil結尾的參數數組,但這太過於誇張。
0
當您需要使用performSelector
發送多個對象時,另一個選項是(如果它很容易做到)修改您希望調用的方法,以NSDictionary
對象代替多個參數,因爲您可以在字典中發送儘可能多的內容。
例如
我有類似這裏面有3個參數的方法,我需要從performSelector稱之爲 -
-(void)getAllDetailsForObjectId:(NSString*)objId segment:(Segment*)segment inContext:(NSManagedObjectContext*)context{
我改變了這種方法來使用字典來存儲參數
-(void)getAllDetailsForObject:(NSDictionary*)details{
因此我能夠很容易地調用該方法
[self performSelector:@selector(getAllDetailsForObject:) withObject:@{Your info stored within a dictionary}];
以爲我會分享這個作爲替代選項,因爲它適用於我。
乾杯
相關問題
- 1. cell.textLabel setText with more than one item
- 2. UILabel with Link with more than one line
- 3. 「Axis more than data dimensions」 - Python
- 4. PHP:if more than ..px resize to
- 5. MySQL MATCH for more than one field
- 6. iOS performSelector onThread with object
- 7. Jasper Report with more page
- 8. ng-repeat with empty objects
- 9. Javascript Set/WeakSet datastructures with objects/arrays
- 10. more than one在$ _get中的一個參數中的值
- 11. html部分跳轉<a name="something"> more than once?
- 12. Spring with JPA not persisting objects
- 13. Polymerfire dom-repeat with Firebase objects
- 14. jquery < than and > than .width
- 15. 在容器中擁有MORE THAN ONE控制器的最佳方式是什麼?
- 16. extjs setValue for textfield not working with objects
- 17. ng-filter with ng-repeat for objects(k,v)
- 18. MVC 3 jquery ajax post 2 objects
- 19. Webapp2 strict_slash返回KeyError:'Missing Argument'for url with trailing slash when method has 2 more args ...錯誤?
- 20. 唧唧to same more more more single single single single single single single single single single single more more??
- 21. windows phone 8 longlistselector selectmore more more
- 22. invove使用performselector
- 23. Javascript array long than 4
- 24. Struts 2 with Thymeleaf
- 25. Collections.sort with 2 array
- 26. jqGrid with Struts 2
- 27. Selenium 2 with liferay
- 28. AFNetworking-2 with Trigger.io
- 29. Lombok with Play 2
- 30. Sentry 2 with ldap
使用objc_msgSend,你需要的#import按:http://stackoverflow.com/questions/4896510/how-to-import-nsobjcruntime-h -to-use-objc-msgsend –
2011-08-25 13:13:02
最後一個注意事項是關於確保所有參數都是id類型的..你可以進一步解釋嗎?或者提供一些資源嗎?我無法找到任何。 – 2012-08-01 07:27:07