1.+ (void)initialize一个类的实例在使用前需要先初始化.
initialize 在一个类中只被调用一次。如果你想为一个类或类的Categories执行独立的初始化,你可以实现load方法。
2.+ (void)load无论何时,只要一个类或者Categories只要被添加到Objective-C的运行时就会调用此方法;加载后实现这个方法来执行类的特殊行为。
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当一个新对象申请了内存空间,就会立刻调用init方法来初始化这个对象。
TheClass *newObject = [[TheClass alloc] init]; alloc和init一起使用
4.- (id)copy返回一个通过copyWithZone:创建的实例。
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当你需要一个实现NSCopying协议的对象时,可以使用此方法。例如,把一个类对象作为NSDictionary的key值
You should not override this method.
6.- (id)mutableCopy当zoon为nil的时候,通过NSMutableCopying 协议方法 mutableCopyWithZone:创建的对象。
对一个采用 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雷同copyWithZone:(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.
1.+ (Class)class返回一个class”对象“
2.+ (Class)superclass返回一个类的父类Class
3.+ (BOOL)isSubclassOfClass:(Class)aClass判断一个类是否是另一个类的子类。
1.+ (BOOL)instancesRespondToSelector:(SEL)aSelector判断一个实例变量是否能够调用一个类的方法
BOOL boolValue = [NSArrayinstancesRespondToSelector:@selector(arrayWithArray:)];返回NO
1.+ (BOOL)conformsToProtocol:(Protocol *)aProtocol判断一个接收者是否能够遵从一个给定的protocol。一个接收者遵从一个协议,可以是直接接受这个协议,也可以继承一个已经接受这个协议的类。
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同 - (IMP)methodForSelector:(SEL)aSelector
3.+ (NSMethodSignature *)instanceMethodSignatureForSelector:(SEL)aSelector返回NSMethodSignature对象,这个对象包含被标示的实例方法的描述。
4.- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector同 + (NSMethodSignature *)instanceMethodSignatureForSelector:(SEL)aSelector
1.+ (NSString *)description以字符串的形式返回对接收者类内容的描述。
1.- (id)autoContentAccessingProxy创建一个接收者对象的代理。
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以默认的方式在主线程中调用一个方法
4.- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait modes:(NSArray *)array同 - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait,只是多了一个可以选择RunLoop模式的参数。
5.- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thread withObject:(id)arg waitUntilDone:(BOOL)wait在指定的线程中执行指定的Selector方法。
6.- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thread withObject:(id)arg waitUntilDone:(BOOL)wait modes:(NSArray *)array是多了一个可以选择RunLoop模式的参数,其它与第5条相同。
7.- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg在一个新的后台线程中调用Selector方法
8.+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget取消执行先前用 performSelector:withObject:afterDelay:这个方法创建的实例方法。取消的只是当前RunLoop里执行的方法而不是所有RunLoop.
performSelector:onThread:withObject:waitUntilDone:和performSelectorInBackground:withObject:执行的方法不能被取消。
9.+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector object:(id)anArgument同第8个方法,只是多了Selector方法和要传递的数据。 但是经测试发现这个方法不能取消performSelector:withObject:afterDelay:执行的方法。(有知道原因的朋友就留言,谢谢)
[self performSelector:@selector(doSomething:) withObject:@"performSelector" afterDelay:1.0]; [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(threadAction) object:nil];
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];
[super dealloc];
}
@end
Car *car = [[Car alloc] init]; NSLog(@"length = %d", [car count]); [car release];car实例本来没有count方法,但是通过消息转发实现了count方法。
1.+ (BOOL)resolveClassMethod:(SEL)name动态的为一个类方法提供了一个SEL实现方法。返回YES表明这个方法已经找到并被添加到接收者,否则返回NO。
2.+ (BOOL)resolveInstanceMethod:(SEL)name动态的为一个实例方法提供了一个SEL实现方法。返回YES表明这个方法已经找到并被添加到接收者,否则返回NO。
@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];具体参考http://blog.csdn.net/namehzf/article/details/6904500 http://blog.csdn.net/ios_developer/article/details/9283199
1.- (void)doesNotRecognizeSelector:(SEL)aSelector处理不被接收者识别的信息。
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.
1.- (void)finalize
1.- (Class)class返回实例变量的Class对象,与 + (Class)class 相同。
2.- (Class)superclass返回父Class,与 +(Class)superclass相同。
1.- (BOOL)isEqual:(id)anObject判断两个对象是否相等。
2.- (NSUInteger)hash返回一个整数,可以在哈希表结构中当作一个地址。如果两个变量isEqual:相等,那么他们的hash值一定相同。
3.- (id)self返回自己。
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同+ (BOOL)conformsToProtocol:(Protocol *) aProtocol
1.- (NSString *)description同 + ( NSString *)description
2.- (NSString *)debugDescription同+ ( NSString *)debugDescription
1.- (id)performSelector:(SEL)aSelector
2.- (id)performSelector:(SEL)aSelector withObject:(id)anObject
3.- (id)performSelector:(SEL)aSelector withObject:(id)anObject withObject:(id)anotherObject带两个参数的。
1.- (BOOL)isProxy判断一个实例不继承自NSObject,如果返回NO就是继承自NSObject,反之返回YES
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)