iOS面试题

iOS 底层原理

2021-02-21  本文已影响0人  smart_M

1.isa指针

image.png image.png

2.runtime 运行时,动态方法交换

//1 动态增加变量
@property (nonatomic, assign) BOOL isNotIgnore;
//runtime 动态绑定 属性
- (BOOL)isNotIgnore{
    //_cmd == @select(isIgnore); 和set方法里一致
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}
- (void)setIsNotIgnore:(BOOL)isNotIgnore{
    // 注意BOOL类型 需要用OBJC_ASSOCIATION_RETAIN_NONATOMIC 不要用错,否则set方法会赋值出错
    objc_setAssociatedObject(self, @selector(isNotIgnore), @(isNotIgnore), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}


//2 对象方法的交换
/**
 *  对象方法的交换
 *
 *  @param anClass    哪个类
 *  @param method1Sel 方法1
 *  @param method2Sel 方法2
 */
+ (void)exchangeInstanceMethod:(Class)anClass method1Sel:(SEL)method1Sel method2Sel:(SEL)method2Sel {
    Method method1 = class_getInstanceMethod(anClass, method1Sel);
    Method method2 = class_getInstanceMethod(anClass, method2Sel);
    method_exchangeImplementations(method1, method2);
}

//3 动态类方法的交换
/**
 *  类方法的交换
 *
 *  @param anClass    哪个类
 *  @param method1Sel 方法1
 *  @param method2Sel 方法2
 */
+ (void)exchangeClassMethod:(Class)anClass method1Sel:(SEL)method1Sel method2Sel:(SEL)method2Sel {
    Method method1 = class_getClassMethod(anClass, method1Sel);
    Method method2 = class_getClassMethod(anClass, method2Sel);
    method_exchangeImplementations(method1, method2);
}


//4 对象方法重置
/**
 *  对象方法重置
 *
 *  @param anClass    哪个类
 *  @param oldMethodSel 方法1
 *  @param newMethodSel 方法2
 */
+ (void)setClassMethod:(Class)anClass oldMethodSel:(SEL) oldMethodSel newMethodSel:(SEL)newMethodSel  {
    Method oldMethod = class_getInstanceMethod(anClass, oldMethodSel);
    Method newMethod = class_getInstanceMethod(anClass, newMethodSel);
    method_setImplementation(oldMethod, method_getImplementation(newMethod));
}

3.Tagged Pointer

3.1 32/64位设备区别

4.Block

4.1 block的底层

4.2 block需要注意的问题

__weak typeof(BaseViewController) *weakSelf = self

5. 多线程GCD

GCD中的三种队列类型

  1. The main queue(主线程串行队列): 与主线程功能相同,提交至Main queue的任务会在主线程中执行,
  1. Global queue(全局并发队列): 全局并发队列由整个进程共享,有高、中(默认)、低、后台四个优先级别。
  1. Custom queue (自定义队列): 可以为串行,也可以为并发。
  1. Group queue (队列组):将多线程进行分组,最大的好处是可获知所有线程的完成情况。
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_sync(mainQueue,^{
  NSLog("MainQueue");            
});

程序一直处于等待状态,block中的代码将执行不到

dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(mainQueue,^{
  NSLog("MainQueue");            
});
dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
   dispatch_async(globalQueue, ^{
       //子线程异步执行下载任务,防止主线程卡顿
       NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
       NSError *error;
       NSString *htmlData = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
       if (htmlData != nil) {
           dispatch_queue_t mainQueue = dispatch_get_main_queue();
            //异步返回主线程,根据获取的数据,更新UI
           dispatch_async(mainQueue, ^{
               NSLog(@"根据更新UI界面");
           });
       } else {
           NSLog(@"error when download:%@",error);
       }
  });

主线程串行队列由系统默认生成的,所以无法调用dispatch_resume()dispatch_suspend()来控制执行继续或中断。

附原文出处(转载):https://my.oschina.net/u/3729367/blog/1606389
https://www.jianshu.com/p/ae786a4cf3b1

上一篇 下一篇

猜你喜欢

热点阅读