iOS Obj-C

發現不少函數經常用,有時熟悉的還是會忘記。

1 respondsToSelector 判斷是否可以執行這個selector

 常用的例子SEL selector = @selector(xxoxx);
    if ([number respondsToSelector:selector]) {
        [number performSelector:selector];
    }

2 Sending Messages
– performSelector: 
– performSelector:withObject: 
– performSelector:withObject:withObject:  

其中下面的代碼做的是同樣的事情:

id myClone = [anObject copy];
id myClone = [anObject performSelector:@selector(copy)];
id myClone = [anObject performSelector:sel_getUid("copy")];

雖然重載了performSelector,但是最多可以接受2個參數,如果需要返回參數和別的參數,可以使用NSInvocation。比如:performSelector:withObject:withObject:。This method is the same as performSelector: except that you can supply two arguments for aSelector. aSelector should identify a method that can take two arguments of type id. For methods with other argument types and return values, use NSInvocation.

3 NSInvocation

NSInvocation objects are used to store and forward messages between objects and between applications, primarily by NSTimer objects and the distributed objects system.
An NSInvocation object contains all the elements of an Objective-C message: a target, a selector, arguments, and the return value. Each of these elements can be set directly, and the return value is set automatically when the NSInvocation object is dispatched.

這個還沒有做實驗,不過從類的說明上已經看出這個很靈活。一個NSInvocation對象可以重複分發給不同的目標。在分發給不同結果中他的參數可以修改。甚至他的selector也可以改變(擁有相同的方法簽名)。這些靈活性使得NSInvocation在重複發送帶多個參數和變量的消息非常有用,而不是簡單的給給每個消息不同的表達式。你僅僅在每一次分發給一個新的target的時候修改NSInvocation對象它所需要的參數。

NSInvocation不支持不定參數或者union參數的函數的回調。(這個自己翻譯的可能不準)。你應該用invocationWithMethodSignature:類方法去創建對象,你不應該用alloc和init去創建。

默認情況,這個類不能對包含回調的參數的計數。如果這些對象可能出現在你創建NSInvaocation實例和你使用的時間內,你應該明確的進行自己對他們進行retain計數或者調用retainArguments方法去使對象自己去計數。

Note:NSInvacation遵循NSCoding協議,但是僅僅支持coding by an NSPortCoder,不支持archiving。

注意參數的index。大於等於2。

getArgument:atIndex:   ---An integer specifying the index of the argument to get.
Indices 0 and 1 indicate the hidden arguments self and _cmd, respectively; these values can be retrieved directly with the target and selector methods.Use indices 2 and greater for the arguments normally passed in a message.

直接網上搜集了一個代碼實例:

動態調用方法時會用到,例子 

-(NSString *)myMethod:(NSString *)param1 withParam2:(NSNumber *)param2 

    NSString *result = @"objc"; 
    NSLog(@"par = %@",param1); 
    NSLog(@"par 2 = %@",param2); 
    return result; 



-(void)invokeMyMethodDynamically 

    SEL selector = @selector(myMethod:withParam2:); 
    NSMethodSignature *methodSignature = [[self class] instanceMethodSignatureForSelector:selector];//獲得類和方法的簽名 
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature]; 
    //從簽名獲得調用對象 
    [invocation setTarget:self]; 
    //設置target 
    [invocation setSelector:selector];//設置selector 
    NSString *returnValue = nil; 
    NSString *argument1 = @"fist"; 
    NSNumber *argument2 = [NSNumber numberWithInt:102]; 
    [invocation setArgument:&argument1 atIndex:2];//設置參數,第一個參數index爲2 
    [invocation setArgument:&argument2 atIndex:3]; 
    [invocation retainArguments];//retain一遍參數 
    [invocation invoke];//調用 
    [invocation getReturnValue:&returnValue];//得到返回值,此時不會再調用,只是返回值 
    NSLog(@"return value = %@",returnValue); 

另外一個例子:

SEL selector = @selector(myMethod:setValue2:);

NSMethodSignature *signature = [MyObject instanceMethodSignatureF orSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSign ature:signature];
[invocation setSelector:selector];

NSString *str1 = @"someString";
NSString *str2 = @"someOtherString";

//The invocation object must retain its arguments
[str1 retain];
[str2 retain];

//Set the arguments
[invocation setTarget:targetInstance];
[invocation setArgument:&str1 atIndex:2];
[invocation setArgument:&str2 atIndex:3];

[NSTimer scheduledTimerWithTimeIn terval:0.1 invocation:invocation repeats:YES]


發佈了74 篇原創文章 · 獲贊 1 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章