0
点赞
收藏
分享

微信扫一扫

2021 iOS高级面试题及答案

1.UITableview的优化方法(缓存高度,异步绘制,减少层级,hide,避免离屏渲染)

NSString *key = [[HeightCache shareHeightCache] makeKeyWithIdentifier:@"YwywProductGradeCell" indexPath:indexPath];
if ([[HeightCache shareHeightCache] existInCacheByKey:key]) {
    return [[HeightCache shareHeightCache] heightFromCacheWithKey:key];
}else{
    YwywProductGradeModelFrame *modelFrame = self.gradeArray[indexPath.row];
    [[HeightCache shareHeightCache] cacheHieght:modelFrame.cellHight key:key];
    return modelFrame.cellHight;
}

2.有没有用过运行时,用它都能做什么?(交换方法,创建类,给新创建的类增加方法,改变isa指针)

   /** 获取原始setBackgroundColor方法 */
Method originalM = class_getInstanceMethod([self class], @selector(setBackgroundColor:));
/** 获取自定义的pb_setBackgroundColor方法 */
Method exchangeM = class_getInstanceMethod([self class], @selector(pb_setBackgroundColor:));
/** 交换方法 */
method_exchangeImplementations(originalM, exchangeM);

创建类:
Class MyClass = objc_allocateClassPair([NSObject class], "Person", 0);

添加方法
/**参数一、类名参数
   二、SEL 添加的方法名字参数
    三、IMP指针 (IMP就是Implementation的缩写,它是指向一个方法实现的指针,每一个方法都有一个对应的IMP)
  参数四、其中types参数为"i@:@“,按顺序分别表示:具体类型可参照[官方文档](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html)i 返回值类型int,若是v则表示void@ 参数id(self): SEL(_cmd)@ id(str)
  V@:表示返回值是void 带有SEL参数 (An object (whether statically typed or typed id))
  */
class_addMethod(Person, @selector(addMethodForMyClass:), (IMP)addMethodForMyClass, "V@:");

添加实例变量
/**参数一、类名参数
  二、属性名称参数
  三、开辟字节长度参数
  四、对其方式参数
  五、参数类型 “@” 官方解释 An object (whether statically typed or typed id) (对象 静态类型或者id类型) 具体类型可参照[官方文档](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html)return: BOOL 是否添加成功
  */
BOOL isSuccess = class_addIvar(Person, "name", sizeof(NSString *), 0, "@");
isSuccess?NSLog(@"添加变量成功"):NSLog(@"添加变量失败");

3.看过哪些第三方框架的源码?都是如何实现的?(如果没有,问一下多图下载的设计)

4.SDWebImage的缓存策略?

5.AFN为什么添加一条常驻线程?

6.KVO的使用?实现原理?(为什么要创建子类来实现)

7.KVC的使用?实现原理?(KVC拿到key以后,是如何赋值的?知不知道集合操作符,能不能访问私有属性,能不能直接访问_ivar)

举报

相关推荐

0 条评论