iOS Obj-C

来源:互联网 发布:淘宝自动秒杀软件 编辑:程序博客网 时间:2024/04/28 10:51

发现不少函数经常用,有时熟悉的还是会忘记。

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]


原创粉丝点击