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);
}
另外一个例子:
NSMethodSignature *signature = [MyObject instanceMethodSignatureF
NSInvocation *invocation = [NSInvocation invocationWithMethodSign
[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
- iOS Obj-C
- iOS Obj-C General Info
- IOS obj-c、c、c++混编
- IOS 收藏 obj-c 编码规范
- 『iOS/Obj-C』Note Of 8-day Obj-C/iOS foundation course
- iOS 的 c++,c,obj-C,swift 混用 (转载)
- 『iOS/Obj-C』V2EX iOS 客户端源码读书笔记
- IOS第七天——Obj-C中的继承
- IOS第十天——Obj-C的属性
- obj-c iOS 数组存入程序文件 arrayWithContentsOfFile
- IOS第十天——Obj-C的属性
- IOS第十天——Obj-C的属性
- iOS项目关于Other Linker Flags添加-Obj-C等
- ios obj-c KVO模式的介绍和使用
- ios 删除线的obj-c实现和swift实现
- Obj-C & webservice
- Obj-C & webservice
- Predicate in Obj-C
- SDCard存储和File存储 的总结
- Struts2 Preparable与ModelDriven
- 虚拟机装苹果系统全过程
- shell下取得字符串的md5值
- ldd3学习笔记:调试技术
- iOS Obj-C
- 精彩文摘
- 看别人的代码不能想当然
- UML图中的符号说明
- 表cache/nocache
- 14.2 以并行方式运行的图形效果
- 创建android下repo的mirror镜像然后内网访问
- 李彦宏谈创业:不被关注是种幸福
- 字符串分割处理,strstr函数应用