NSObject Class Reference
Initializing a Class
- 1.+ (void)initialize
initialize 在一个类中只被调用一次。如果你想为一个类或类的Categories执行独立的初始化,你可以实现load方法。
- 2.+ (void)load
Creating, Copying, and Deallocating Objects
- 1.+ (id)alloc
- 2.+ (id)allocWithZone:(NSZone *)zone
This method exists for historical reasons; memory zones are no longer used by Objective-C.
- 3.- (id)init
在NSObject类中定义的init方法没有初始化,只是返回了self指针。
TheClass *newObject = [[TheClass alloc] init]; alloc和init一起使用
- 4.- (id)copy
This is a convenience method for classes that adopt the NSCopying protocol. An exception is raised if there is no implementation forcopyWithZone:.
NSObject does not itself support the NSCopying protocol. Subclasses must support the protocol and implement thecopyWithZone: method. A subclass version of thecopyWithZone: method should send the message tosuper first, to incorporate its implementation, unless the subclass descends directly fromNSObject.
- 5.+ (id)copyWithZone:(NSZone *)zone
You should not override this method.
- 6.- (id)mutableCopy
对一个采用 NSMutableCopying协议的类来说这是一个方便的方法。如果没有实现mutableCopyWithZone:的话就会抛异常。
This is a convenience method for classes that adopt the NSMutableCopying protocol. An exception is raised if there is no implementation formutableCopyWithZone:.
- 7.+ (id)mutableCopyWithZone:(NSZone *)zone
- 8.+ (id)new
分配一个实例新的内存空间,调用init方法,最后返回一个初始化过的对象。
This method is a combination of alloc andinit. Likealloc, it initializes theisa instance variable of the new object so it points to the class data structure. It then invokes theinit method to complete the initialization process.
Identifying Classes
- 1.+ (Class)class
- 2.+ (Class)superclass
- 3.+ (BOOL)isSubclassOfClass:(Class)aClass
Testing Class Functionality
- 1.+ (BOOL)instancesRespondToSelector:(SEL)aSelector
比如,
BOOL boolValue = [NSArrayinstancesRespondToSelector:@selector(arrayWithArray:)];返回NO
Testing Protocol Conformance
- 1.+ (BOOL)conformsToProtocol:(Protocol *)aProtocol
Obtaining Information About Methods
- 1.- (IMP)methodForSelector:(SEL)aSelector
定位并且返回接收者实现该方法的地址,因此可以作为一个方法来调用。
传入参数aSelector必须是有效切非空的,可以用respondsToSelector: 这个方法提前判断。
- @interface TestIMP : NSObject
- - (void)saySomething;
- @end
- @implementation TestIMP
- - (void)saySomething
- {
- NSLog(@"invoke saySomething");
- }
- @end
- // For Test
- TestIMP *testIMP = [[TestIMP alloc] init];
- SEL aSelector = @selector(saySomething);
- IMP imp = [testIMP methodForSelector:aSelector];
- imp(testIMP, aSelector);
- 2.+ (IMP)instanceMethodForSelector:(SEL)aSelector
- 3.+ (NSMethodSignature *)instanceMethodSignatureForSelector:(SEL)aSelector
- 4.- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
Describing Objects
- 1.+ (NSString *)description
Discardable Content Proxy Support
- 1.- (id)autoContentAccessingProxy
Sending Messages
- 1.- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay
- 2.- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay inModes:(NSArray *)modes
anArgument
如果想把需要的数据传给Selector方法中,需要设置anArgument参数,对应Selector就应是带参数的方法
modes- [self performSelector:@selector(doSomething:)
- withObject:[NSArray arrayWithObjects:@"AObject", @"BObject", nil]
- afterDelay:1.0
- inModes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, nil]];
- - (void)doSomething:(id)sender
- {
- NSLog(@"invoke doSomething");
- NSLog(@"%@", sender);
- }
- 3.- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait
如果设置为YES那么将阻塞当前线程,设置为NO将立刻调用Selector方法
- 4.- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait modes:(NSArray *)array
- 5.- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thread withObject:(id)arg waitUntilDone:(BOOL)wait
- 6.- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thread withObject:(id)arg waitUntilDone:(BOOL)wait modes:(NSArray *)array
- 7.- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg
- 8.+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget
performSelector:onThread:withObject:waitUntilDone:和performSelectorInBackground:withObject:执行的方法不能被取消。
- 9.+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector object:(id)anArgument
测试方法如下:
- [self performSelector:@selector(doSomething:)
- withObject:@"performSelector"
- afterDelay:1.0];
- [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(threadAction) object:nil];
Forwarding Messages
- 1.- (id)forwardingTargetForSelector:(SEL)aSelector
- 2.- (void)forwardInvocation:(NSInvocation *)anInvocation
- @interface Car : NSObject
- {
- NSString *_name;
- NSArray *_array;;
- }
- @end
- #import "Car.h"
- @implementation Car
- - (id)init
- {
- self = [super init];
- if (self) {
- _name = [[NSString alloc] initWithString:@"I AM A CAR~~"];
- _array = [[NSArray alloc] initWithObjects:@"1", @"2", nil];
- }
- return self;
- }
- - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
- {
- NSMethodSignature* signature = [super methodSignatureForSelector:aSelector];
- if (signature == nil)
- {
- signature = [_array methodSignatureForSelector:aSelector];
- }
- NSUInteger argCount = [signature numberOfArguments];
- for (NSInteger i=0 ; i<argCount ; i++)
- {
- NSLog(@"%s" , [signature getArgumentTypeAtIndex:i]);
- }
- NSLog(@"methodReturnType:%s, methodReturnLength:%d" , [signature methodReturnType] , [signature methodReturnLength]);
- NSLog(@"signature:%@" , signature);
- return signature;
- }
- - (void)forwardInvocation:(NSInvocation *)anInvocation
- {
- SEL seletor = [anInvocation selector];
- if ([_array respondsToSelector:seletor])
- {
- [anInvocation invokeWithTarget:_array];
- }
- else
- {
- [super forwardInvocation:anInvocation];
- }
- }
- - (id)forwardingTargetForSelector:(SEL)aSelector
- {
- NSLog(@"forwardingTargetForSelector");
- if ([_array respondsToSelector:aSelector])
- {
- return _array;
- }
- return nil;
- }
- - (void)dealloc
- {
- [_name release];
- [_array release];
- <span style="white-space:pre"> </span>[super dealloc];
- }
- @end
测试方法如下:
- Car *car = [[Car alloc] init];
- NSLog(@"length = %d", [car count]);
- [car release];
部分参考http://blog.csdn.net/freshforiphone/article/details/7381329
Dynamically Resolving Methods
- 1.+ (BOOL)resolveClassMethod:(SEL)name
- 2.+ (BOOL)resolveInstanceMethod:(SEL)name
实例如下:
- @interface Person : NSObject
- {
- NSString *name;
- float weight;
- }
- @property (retain,readwrite) NSString* name;
- @property (readonly)float weight;
- @property float height;
- -(Person*) initWithWeight: (int) weight;
- -(void) print: (NSString*) str;
- @end
- void dynamicMethod(id self,SEL _cmd,float w)
- {
- printf("dynamicMethod-%s\n",[NSStringFromSelector(_cmd) cStringUsingEncoding:NSUTF8StringEncoding]);
- printf("%f\n",w);
- }
- @implementation Person
- @synthesize name;
- @synthesize weight;
- @dynamic height; // 注意这里
- // 在实现类中使用了@dynamic指令
- -(Person*) initWithWeight: (int) w
- {
- self=[super init];
- if (self)
- {
- weight=w;
- }
- return self;
- }
- -(void) print: (NSString*) str
- {
- NSLog(@"%@%@",str,name);
- }
- + (BOOL) resolveInstanceMethod: (SEL) sel
- {
- NSString *methodName=NSStringFromSelector(sel);
- BOOL result=NO;
- //看看是不是我们要动态实现的方法名称
- if ([methodName isEqualToString:@"setHeight:"])
- {
- class_addMethod([self class], sel, (IMP) dynamicMethod,"v@:f");
- result=YES;
- }
- return result;
- }
- + (BOOL)resolveClassMethod:(SEL)sel
- {
- NSString *methodName=NSStringFromSelector(sel);
- BOOL result=NO;
- //看看是不是我们要动态实现的方法名称
- if ([methodName isEqualToString:@"setHeight:"])
- {
- class_addMethod([self class], sel, (IMP) dynamicMethod,"v@:f");
- result=YES;
- }
- return result;
- }
- -(void) dealloc
- {
- [self setName:nil];
- [super dealloc];
- }
- @end
测试方法如下:
- Person *person = [[Person alloc] init];
- [person setHeight:100.0];
Error Handling
- 1.- (void)doesNotRecognizeSelector:(SEL)aSelector
Archiving
- 1.- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder
- 2.- (Class)classForCoder
- 3.- (Class)classForKeyedArchiver
- 4.+ (NSArray *)classFallbacksForKeyedArchiver
- 5.+ (Class)classForKeyedUnarchiver
- 6.- (id)replacementObjectForCoder:(NSCoder *)aCoder
- 7.- (id)replacementObjectForCoder:(NSCoder *)aCoder
- 8.+ (void)setVersion:(NSInteger)aVersion
- 9.+ (NSInteger)version
Don’t simply send version to the return value of class—a subclass version number may be returned instead.
Deprecated Methods
Identifying Classes
- 1.- (Class)class
- 2.- (Class)superclass
Identifying and Comparing Objects
- 1.- (BOOL)isEqual:(id)anObject
- 2.- (NSUInteger)hash
- 3.- (id)self
Testing Object Inheritance, Behavior, and Conformance
- 1.- (BOOL)isKindOfClass:(Class)aClass
- 可以这样来使用:
- ShakeViewController *shake = [[ShakeViewController alloc] init];
- BOOL boolValue = [shake isKindOfClass:[NSObject class]];
- // DO NOT DO THIS!
- if ([myArray isKindOfClass:[NSMutableArray class]])
- {
- // Modify the object
- }
- 2.- (BOOL)isMemberOfClass:(Class)aClass
- BOOL boolValue = [shake isMemberOfClass:[NSObject class]];
- boolValue=NO,不支持继承类的判断
- 3.- (BOOL)respondsToSelector:(SEL)aSelector
- ShakeViewController *shake = [[ShakeViewController alloc] init];
- BOOL boolValue = [shake respondsToSelector:@selector(parentViewController)];
- 返回YES。
- BOOL boolValue = [[shake superclass] respondsToSelector:@selector(parentViewController)];
- 返回NO。
- BOOL boolValue = [ShakeViewController instancesRespondToSelector:@selector(parentViewController)];
- 返回YES。
- BOOL boolValue = [[shake superclass] instancesRespondToSelector:@selector(parentViewController)];
- 返回YES。
- 4.- (BOOL)conformsToProtocol:(Protocol *)aProtocol
Describing Objects
- 1.- (NSString *)description
- 2.- (NSString *)debugDescription
Sending Messages
- 1.- (id)performSelector:(SEL)aSelector
- 2.- (id)performSelector:(SEL)aSelector withObject:(id)anObject
- 3.- (id)performSelector:(SEL)aSelector withObject:(id)anObject withObject:(id)anotherObject
Identifying Proxies
- 1.- (BOOL)isProxy
Obsolete Methods
- 1.- (id)retain
- 2.- (oneway void)release
- 3.- (id)autorelease
Decrements the receiver’s retain count at the end of the current autorelease pool block. (required)
- 4.- (NSUInteger)retainCount
- 5.- (NSZone *)zone
Zones are deprecated and ignored by most classes that have it as a parameter. (required)