【OC梳理】NSObject

NSObject

NSObject是Foundation框架中的基础类,OC中常用的类绝大多数都继承于它,提供了包括alloc、init等方法供我们使用:

  • isEqual: 判断两个指针变量是否指向同一个对象,与直接用“==”来比较的效果相同。
  • isMemberOfClass: 判断一个指针变量指向的对象是不是属于一个类创建的,用于判断是否某个特定类,如:
UIView *view = [[UIView alloc]init];
BOOL isBelongToView = [view isMemberOfClass:[UIView class]];
BOOL isBelongToObject = [view isMemberOfClass:[NSObject class]];
NSLog(@"%d   %d",isBelongToView,isBelongToObject);

输出的结果为 :

 1   0
  • isKindOfClass: 判断一个指针变量指向的对象是不是一个类的子类,如:
UIView *view = [[UIView alloc]init];
BOOL isBelongToView = [view isKindOfClass:[UIView class]];
BOOL isBelongToObject = [view isKindOfClass:[NSObject class]];
NSLog(@"%d   %d",isBelongToView,isBelongToObject);

其输出结果为:

 1   1
  • respondsToSelector: 判断一个类中是否有某个方法的实现,只要在.m中有实现,就返回YES,与是否私有方法无关。
  • performSelector方法
    包括以下方法:
//直接调用某个方法,以下分别为0、1、2个参数时的方法
- (id)performSelector:(SEL)aSelector;
- (id)performSelector:(SEL)aSelector withObject:(id)object;
- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;
//延时调用某个方法(异步执行,只能在主线程中执行。)
- (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay inModes:(NSArray *)modes;
- (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay;
//取消已设置的延时调用,需要注意参数的一致性
+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector object:(nullable id)anArgument;
+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget;
//主线程运行某个方法,如果设置wait为YES:等待当前线程执行完以后,主线程才会执行aSelector方法。当前线程就是主线程,那么aSelector方法会马上执行。
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray *)array;
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait;
//指定线程中调用某个方法
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray *)array NS_AVAILABLE(10_5, 2_0);
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait NS_AVAILABLE(10_5, 2_0);
//后台调用
- (void)performSelectorInBackground:(SEL)aSelector withObject:(nullable id)arg NS_AVAILABLE(10_5, 2_0);
//按order顺序执行多个performSelector调用(类似队列)
- (void)performSelector:(SEL)aSelector target:(id)target argument:(nullable id)arg order:(NSUInteger)order modes:(NSArray *)modes;
//取消已设置的performSelector调用
- (void)cancelPerformSelector:(SEL)aSelector target:(id)target argument:(nullable id)arg;
- (void)cancelPerformSelectorsWithTarget:(id)target;

你可能感兴趣的:(【OC梳理】NSObject)