标签:ios
@interface Student:NSObject -(void)go; -(void)showName:(NSString *)name; -(void)introduce:(NSString *)name :(NSString*)address; @end Student* stu = [[Student alloc]init];
- (id)copyWithZone:(NSZone *)zone; - (id)mutableCopyWithZone:(NSZone *)zone;
<pre name="code" class="objc">@interface Student:NSObject <NSCopying> @end @interface Student (id)copyWithZone:(NSZone *)zone { // 创建的副本对象不需要释放,有调用者释放 // [self class]防止继承之后,返回父类对象 Student* copy = [[[self class] allocWithZone:zone] init]; return copy; } @end
// 相当于[stu go]; [stu performSelector:@selector(go)]; // 相当于[stu showName:@"Jun"]; [stu performSelector:@selector(showName:) withObjct:@"Jun"]; 相当于[stu introduce:@"Jun":@"address"]; [stu performSelector:@selector(introduce::) withObjct:@"Jun" withObjct:@"address"]; // 延时2秒之后调用 [stu performSelector:@selector(showName:) withObjct:@"Jun" afterDelay:2];
// 字符串类名到类 NSString* className = @"Student"; Class class = NSClassFromString(className); Person* p = [[class alloc] init]; // 类到字符串类名 NSString* strClass = NSStringFromClass([Person class]);
// 字符串方法名到方法 NSString* funName = @"go"; SEL selector = NSSelectorFromString(funName); [stu performSelector:selector]; // 方法到字符串方法名 NSString* strFun = NSStringFromSelector(selector);
@interface NSObject <NSObject> { Class isa; }
typedef struct objc_class *Class;
struct objc_class { Class isa; };
- (BOOL)isMemberOfClass:(Class)cls { return [self class] == cls; } - (Class)class { return object_getClass(self); } Class object_getClass(id obj) { return _object_getClass(obj); } static inline Class _object_getClass(id obj) { if (obj) return obj->isa; else return Nil; }
标签:ios
原文地址:http://blog.csdn.net/xufeng0991/article/details/43534811