美文网首页Optional Swift编程珠玑iOS DeveloperiOS面试题整理
阿里、字节 一套高效的iOS面试题解答(持续更新)

阿里、字节 一套高效的iOS面试题解答(持续更新)

作者: NinthDay | 来源:发表于2020-03-16 01:09 被阅读0次

    [TOC]

    runtime相关问题

    面试题出自掘金的一篇文章《阿里、字节:一套高效的iOS面试题》

    结构模型

    1. 介绍下runtime的内存模型(isa、对象、类、metaclass、结构体的存储信息等)

    2. 为什么要设计metaclass

    3. class_copyIvarList & class_copyPropertyList区别

    class_copyIvarList 获取类对象中的所有实例变量信息,从 class_ro_t 中获取:

    Ivar *
    class_copyIvarList(Class cls, unsigned int *outCount)
    {
        const ivar_list_t *ivars;
        Ivar *result = nil;
        unsigned int count = 0;
    
        if (!cls) {
            if (outCount) *outCount = 0;
            return nil;
        }
    
        mutex_locker_t lock(runtimeLock);
    
        assert(cls->isRealized());
        
        if ((ivars = cls->data()->ro->ivars)  &&  ivars->count) {
            result = (Ivar *)malloc((ivars->count+1) * sizeof(Ivar));
            
            for (auto& ivar : *ivars) {
                if (!ivar.offset) continue;  // anonymous bitfield
                result[count++] = &ivar;
            }
            result[count] = nil;
        }
        
        if (outCount) *outCount = count;
        return result;
    }
    

    class_copyPropertyList 获取类对象中的属性信息, class_rw_tproperties,先后输出了 category / extension/ baseClass 的属性,而且仅输出当前的类的属性信息,而不会向上去找 superClass 中定义的属性。

    objc_property_t *
    class_copyPropertyList(Class cls, unsigned int *outCount)
    {
        if (!cls) {
            if (outCount) *outCount = 0;
            return nil;
        }
    
        mutex_locker_t lock(runtimeLock);
    
        checkIsKnownClass(cls);
        assert(cls->isRealized());
        
        auto rw = cls->data();
    
        property_t **result = nil;
        unsigned int count = rw->properties.count();
        if (count > 0) {
            result = (property_t **)malloc((count + 1) * sizeof(property_t *));
    
            count = 0;
            for (auto& prop : rw->properties) {
                result[count++] = ∝
            }
            result[count] = nil;
        }
    
        if (outCount) *outCount = count;
        return (objc_property_t *)result;
    }
    

    Q1: class_ro_t 中的 baseProperties 呢?

    Q2: class_rw_t 中的 properties 包含了所有属性,那何时注入进去的呢? 答案见 5.

    4. class_rw_tclass_ro_t 的区别

    class_rw_t_class_ro_t.png

    测试发现,class_rw_t 中的 properties 属性按顺序包含分类/扩展/基类中的属性。

    struct class_ro_t {
        uint32_t flags;
        uint32_t instanceStart;
        uint32_t instanceSize;
    #ifdef __LP64__
        uint32_t reserved;
    #endif
    
        const uint8_t * ivarLayout;
        
        const char * name;
        method_list_t * baseMethodList;
        protocol_list_t * baseProtocols;
        const ivar_list_t * ivars;
    
        const uint8_t * weakIvarLayout;
        property_list_t *baseProperties;
    
        method_list_t *baseMethods() const {
            return baseMethodList;
        }
    };
    
    struct class_rw_t {
        // Be warned that Symbolication knows the layout of this structure.
        uint32_t flags;
        uint32_t version;
    
        const class_ro_t *ro;
    
        method_array_t methods;
        property_array_t properties;
        protocol_array_t protocols;
    
        Class firstSubclass;
        Class nextSiblingClass;
    
        char *demangledName;
    
    #if SUPPORT_INDEXED_ISA
        uint32_t index;
    #endif
    }
    

    5. category如何被加载的,两个category的load方法的加载顺序,两个category的同名方法的加载顺序

    ... -> realizeClass -> methodizeClass(用于Attach categories)-> attachCategories 关键就是在 methodizeClass 方法实现中

    static void methodizeClass(Class cls)
    {
        runtimeLock.assertLocked();
    
        bool isMeta = cls->isMetaClass();
        auto rw = cls->data();
        auto ro = rw->ro;
        
        // =======================================
            // 省略.....
        // =======================================
      
        property_list_t *proplist = ro->baseProperties;
        if (proplist) {
            rw->properties.attachLists(&proplist, 1);
        }
    
        // =======================================
            // 省略.....
        // =======================================
    
        // Attach categories.
        category_list *cats = unattachedCategoriesForClass(cls, true /*realizing*/);
        attachCategories(cls, cats, false /*don't flush caches*/);
    
        // =======================================
            // 省略.....
        // =======================================
        
        if (cats) free(cats);
    
    }
    

    上面代码能确定 baseProperties 在前,category 在后,但决定顺序的是 rw->properties.attachLists 这个方法:

    property_list_t *proplist = ro->baseProperties;
    if (proplist) {
      rw->properties.attachLists(&proplist, 1);
    }
    
    /// category 被附加进去
    void attachLists(List* const * addedLists, uint32_t addedCount) {
            if (addedCount == 0) return;
    
            if (hasArray()) {
                // many lists -> many lists
                uint32_t oldCount = array()->count;
                uint32_t newCount = oldCount + addedCount;
                setArray((array_t *)realloc(array(), array_t::byteSize(newCount)));
                array()->count = newCount;
                
                // 将旧内容移动偏移量 addedCount 然后将 addedLists copy 到起始位置
                /*
                    struct array_t {
                            uint32_t count;
                            List* lists[0];
                            };
                */
                memmove(array()->lists + addedCount, array()->lists, 
                        oldCount * sizeof(array()->lists[0]));
                memcpy(array()->lists, addedLists, 
                       addedCount * sizeof(array()->lists[0]));
            }
            else if (!list  &&  addedCount == 1) {
                // 0 lists -> 1 list
                list = addedLists[0];
            } 
            else {
                // 1 list -> many lists
                List* oldList = list;
                uint32_t oldCount = oldList ? 1 : 0;
                uint32_t newCount = oldCount + addedCount;
                setArray((array_t *)malloc(array_t::byteSize(newCount)));
                array()->count = newCount;
                if (oldList) array()->lists[addedCount] = oldList;
                memcpy(array()->lists, addedLists, 
                       addedCount * sizeof(array()->lists[0]));
            }
        }
    

    所以 category 的属性总是在前面的,baseClass的属性被往后偏移了。

    Q1:那么多个 category 的顺序呢?答案见6

    6. category & extension区别,能给NSObject添加Extension吗,结果如何

    category:

    • 运行时添加分类属性/协议/方法
    • 分类添加的方法会“覆盖”原类方法,因为方法查找的话是从头至尾,一旦查找到了就停止了
    • 同名分类方法谁生效取决于编译顺序,image 读取的信息是倒叙的,所以编译越靠后的越先读入
    • 名字相同的分类会引起编译报错;

    extension:

    • 编译时决议
    • 只以声明的形式存在,多数情况下就存在于 .m 文件中;
    • 不能为系统类添加扩展

    7. 消息转发机制,消息转发机制和其他语言的消息机制优劣对比

    8. 在方法调用的时候,方法查询-> 动态解析-> 消息转发 之前做了什么

    9. IMPSELMethod的区别和使用场景

    三者的定义:

    typedef struct method_t *Method;
    
    using MethodListIMP = IMP;
    
    struct method_t {
        SEL name;
        const char *types;
        MethodListIMP imp;
    };
    

    Method 同样是个对象,封装了方法名和实现,关于 Type Encodings

    Code Meaning
    c A char
    i An int
    s A short
    l A long``l is treated as a 32-bit quantity on 64-bit programs.
    q A long long
    C An unsigned char
    I An unsigned int
    S An unsigned short
    L An unsigned long
    Q An unsigned long long
    f A float
    d A double
    B A C++ bool or a C99 _Bool
    v A void
    * A character string (char *)
    @ An object (whether statically typed or typed id)
    # A class object (Class)
    : A method selector (SEL)
    [array type] An array
    {name=type...} A structure
    (name=type...) A union
    bnum A bit field of num bits
    ^type A pointer to type
    ? An unknown type (among other things, this code is used for function pointers)

    -(void)hello:(NSString *)name encode 下就是 v@:@

    10. loadinitialize方法的区别什么?在继承关系中他们有什么区别

    load 方法调用时机,而且只调用当前类本身,不会调用superClass 的 +load 方法:

    void
    load_images(const char *path __unused, const struct mach_header *mh)
    {
        // Return without taking locks if there are no +load methods here.
        if (!hasLoadMethods((const headerType *)mh)) return;
    
        recursive_mutex_locker_t lock(loadMethodLock);
    
        // Discover load methods
        {
            mutex_locker_t lock2(runtimeLock);
            prepare_load_methods((const headerType *)mh);
        }
    
        // Call +load methods (without runtimeLock - re-entrant)
        call_load_methods();
    }
    
    void call_load_methods(void)
    {
        static bool loading = NO;
        bool more_categories;
    
        loadMethodLock.assertLocked();
    
        // Re-entrant calls do nothing; the outermost call will finish the job.
        if (loading) return;
        loading = YES;
    
        void *pool = objc_autoreleasePoolPush();
    
        do {
            // 1. Repeatedly call class +loads until there aren't any more
            while (loadable_classes_used > 0) {
                call_class_loads();
            }
    
            // 2. Call category +loads ONCE
            more_categories = call_category_loads();
    
            // 3. Run more +loads if there are classes OR more untried categories
        } while (loadable_classes_used > 0  ||  more_categories);
    
        objc_autoreleasePoolPop(pool);
    
        loading = NO;
    }
    

    +initialize 实现

    void _class_initialize(Class cls)
    {
        assert(!cls->isMetaClass());
    
        Class supercls;
        bool reallyInitialize = NO;
    
        // Make sure super is done initializing BEFORE beginning to initialize cls.
        // See note about deadlock above.
        supercls = cls->superclass;
        if (supercls  &&  !supercls->isInitialized()) {
            _class_initialize(supercls);
        }
        
        // Try to atomically set CLS_INITIALIZING.
        {
            monitor_locker_t lock(classInitLock);
            if (!cls->isInitialized() && !cls->isInitializing()) {
                cls->setInitializing();
                reallyInitialize = YES;
            }
        }
        
        if (reallyInitialize) {
            // We successfully set the CLS_INITIALIZING bit. Initialize the class.
            
            // Record that we're initializing this class so we can message it.
            _setThisThreadIsInitializingClass(cls);
    
            if (MultithreadedForkChild) {
                // LOL JK we don't really call +initialize methods after fork().
                performForkChildInitialize(cls, supercls);
                return;
            }
            
            // Send the +initialize message.
            // Note that +initialize is sent to the superclass (again) if 
            // this class doesn't implement +initialize. 2157218
            if (PrintInitializing) {
                _objc_inform("INITIALIZE: thread %p: calling +[%s initialize]",
                             pthread_self(), cls->nameForLogging());
            }
    
            // Exceptions: A +initialize call that throws an exception 
            // is deemed to be a complete and successful +initialize.
            //
            // Only __OBJC2__ adds these handlers. !__OBJC2__ has a
            // bootstrapping problem of this versus CF's call to
            // objc_exception_set_functions().
    #if __OBJC2__
            @try
    #endif
            {
                callInitialize(cls);
    
                if (PrintInitializing) {
                    _objc_inform("INITIALIZE: thread %p: finished +[%s initialize]",
                                 pthread_self(), cls->nameForLogging());
                }
            }
    #if __OBJC2__
            @catch (...) {
                if (PrintInitializing) {
                    _objc_inform("INITIALIZE: thread %p: +[%s initialize] "
                                 "threw an exception",
                                 pthread_self(), cls->nameForLogging());
                }
                @throw;
            }
            @finally
    #endif
            {
                // Done initializing.
                lockAndFinishInitializing(cls, supercls);
            }
            return;
        }
        
        else if (cls->isInitializing()) {
            // We couldn't set INITIALIZING because INITIALIZING was already set.
            // If this thread set it earlier, continue normally.
            // If some other thread set it, block until initialize is done.
            // It's ok if INITIALIZING changes to INITIALIZED while we're here, 
            //   because we safely check for INITIALIZED inside the lock 
            //   before blocking.
            if (_thisThreadIsInitializingClass(cls)) {
                return;
            } else if (!MultithreadedForkChild) {
                waitForInitializeToComplete(cls);
                return;
            } else {
                // We're on the child side of fork(), facing a class that
                // was initializing by some other thread when fork() was called.
                _setThisThreadIsInitializingClass(cls);
                performForkChildInitialize(cls, supercls);
            }
        }
        
        else if (cls->isInitialized()) {
            // Set CLS_INITIALIZING failed because someone else already 
            //   initialized the class. Continue normally.
            // NOTE this check must come AFTER the ISINITIALIZING case.
            // Otherwise: Another thread is initializing this class. ISINITIALIZED 
            //   is false. Skip this clause. Then the other thread finishes 
            //   initialization and sets INITIALIZING=no and INITIALIZED=yes. 
            //   Skip the ISINITIALIZING clause. Die horribly.
            return;
        }
        
        else {
            // We shouldn't be here. 
            _objc_fatal("thread-safe class init in objc runtime is buggy!");
        }
    }
    
    void callInitialize(Class cls)
    {
        ((void(*)(Class, SEL))objc_msgSend)(cls, SEL_initialize);
        asm("");
    }
    

    注意看上面的调用了 callInitialize(cls) 然后又调用了 lockAndFinishInitializing(cls, supercls)

    摘自iOS App冷启动治理 一文中对 Dyld 在各阶段所做的事情:

    阶段 工作
    加载动态库 Dyld从主执行文件的header获取到需要加载的所依赖动态库列表,然后它需要找到每个 dylib,而应用所依赖的 dylib 文件可能会再依赖其他 dylib,所以所需要加载的是动态库列表一个递归依赖的集合
    Rebase和Bind - Rebase在Image内部调整指针的指向。在过去,会把动态库加载到指定地址,所有指针和数据对于代码都是对的,而现在地址空间布局是随机化,所以需要在原来的地址根据随机的偏移量做一下修正 - Bind是把指针正确地指向Image外部的内容。这些指向外部的指针被符号(symbol)名称绑定,dyld需要去符号表里查找,找到symbol对应的实现
    Objc setup - 注册Objc类 (class registration) - 把category的定义插入方法列表 (category registration) - 保证每一个selector唯一 (selector uniquing)
    Initializers - Objc的+load()函数 - C++的构造函数属性函数 - 非基本类型的C++静态全局变量的创建(通常是类或结构体)

    最后 dyld 会调用 main() 函数,main() 会调用 UIApplicationMain(),before main()的过程也就此完成。

    11. 说说消息转发机制的优劣

    内存管理

    1. weak的实现原理?SideTable的结构是什么样的
    2. 关联对象的应用?系统如何实现关联对象的
    3. 关联对象的如何进行内存管理的?关联对象如何实现weak属性
    4. Autoreleasepool的原理?所使用的的数据结构是什么
    5. ARC的实现原理?ARC下对retain & release做了哪些优化
    6. ARC下哪些情况会造成内存泄漏

    其他

    1. Method Swizzle注意事项
    2. 属性修饰符atomic的内部实现是怎么样的?能保证线程安全吗
    3. iOS 中内省的几个方法有哪些?内部实现原理是什么
    4. class、objc_getClass、object_getclass 方法有什么区别?

    NSNotification相关

    认真研读、你可以在这里找到答案轻松过面:一文全解iOS通知机制(经典收藏)

    1. 实现原理(结构设计、通知如何存储的、name&observer&SEL之间的关系等)
    2. 通知的发送时同步的,还是异步的
    3. NSNotificationCenter接受消息和发送消息是在一个线程里吗?如何异步发送消息
    4. NSNotificationQueue是异步还是同步发送?在哪个线程响应
    5. NSNotificationQueuerunloop的关系
    6. 如何保证通知接收的线程在主线程
    7. 页面销毁时不移除通知会崩溃吗
    8. 多次添加同一个通知会是什么结果?多次移除通知呢
    9. 下面的方式能接收到通知吗?为什么
    // 发送通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"TestNotification" object:@1];
    // 接收通知
    [NSNotificationCenter.defaultCenter postNotificationName:@"TestNotification" object:nil];
    复制代码
    

    Runloop & KVO

    runloop

    runloop对于一个标准的iOS开发来说都不陌生,应该说熟悉runloop是标配,下面就随便列几个典型问题吧

    1. app如何接收到触摸事件的
    2. 为什么只有主线程的runloop是开启的
    3. 为什么只在主线程刷新UI
    4. PerformSelectorrunloop的关系
    5. 如何使线程保活

    KVO

    runloop一样,这也是标配的知识点了,同样列出几个典型问题

    1. 实现原理
    2. 如何手动关闭kvo
    3. 通过KVC修改属性会触发KVO么
    4. 哪些情况下使用kvo会崩溃,怎么防护崩溃
    5. kvo的优缺点

    Block

    1. block的内部实现,结构体是什么样的
    2. block是类吗,有哪些类型
    3. 一个int变量被 __block 修饰与否的区别?block的变量截获
    4. block在修改NSMutableArray,需不需要添加__block
    5. 怎么进行内存管理的
    6. block可以用strong修饰吗
    7. 解决循环引用时为什么要用__strong、__weak修饰
    8. block发生copy时机
    9. Block访问对象类型的auto变量时,在ARC和MRC下有什么区别

    多线程

    主要以GCD为主

    1. iOS开发中有多少类型的线程?分别对比
    2. GCD有哪些队列,默认提供哪些队列
    3. GCD有哪些方法api
    4. GCD主线程 & 主队列的关系
    5. 如何实现同步,有多少方式就说多少
    6. dispatch_once实现原理
    7. 什么情况下会死锁
    8. 有哪些类型的线程锁,分别介绍下作用和使用场景
    9. NSOperationQueue中的maxConcurrentOperationCount默认值
    10. NSTimer、CADisplayLink、dispatch_source_t 的优劣

    视图&图像相关

    1. AutoLayout的原理,性能如何
    2. UIView & CALayer的区别
    3. 事件响应链
    4. drawrect & layoutsubviews调用时机
    5. UI的刷新原理
    6. 隐式动画 & 显示动画区别
    7. 什么是离屏渲染
    8. imageName & imageWithContentsOfFile区别
    9. 多个相同的图片,会重复加载吗
    10. 图片是什么时候解码的,如何优化
    11. 图片渲染怎么优化
    12. 如果GPU的刷新率超过了iOS屏幕60Hz刷新率是什么现象,怎么解决

    性能优化

    1. 如何做启动优化,如何监控
    2. 如何做卡顿优化,如何监控
    3. 如何做耗电优化,如何监控
    4. 如何做网络优化,如何监控

    开发证书

    1. 苹果使用证书的目的是什么
    2. AppStore安装app时的认证流程
    3. 开发者怎么在debug模式下把app安装到设备呢

    架构设计

    典型源码的学习

    只是列出一些iOS比较核心的开源库,这些库包含了很多高质量的思想,源码学习的时候一定要关注每个框架解决的核心问题是什么,还有它们的优缺点,这样才能算真正理解和吸收

    1. AFN
    2. SDWebImage
    3. JSPatch、Aspects(虽然一个不可用、另一个不维护,但是这两个库都很精炼巧妙,很适合学习)
    4. Weex/RN, 笔者认为这种前端和客户端紧密联系的库是必须要知道其原理的
    5. CTMediator、其他router库,这些都是常见的路由库,开发中基本上都会用到
    6. 圈友们在评论下面补充吧

    架构设计

    1. 手动埋点、自动化埋点、可视化埋点
    2. MVC、MVP、MVVM设计模式
    3. 常见的设计模式
    4. 单例的弊端
    5. 常见的路由方案,以及优缺点对比
    6. 如果保证项目的稳定性
    7. 设计一个图片缓存框架(LRU)
    8. 如何设计一个git diff
    9. 设计一个线程池?画出你的架构图
    10. 你的app架构是什么,有什么优缺点、为什么这么做、怎么改进

    其他问题

    1. PerformSelector & NSInvocation优劣对比
    2. oc怎么实现多继承?怎么面向切面(可以参考Aspects深度解析-iOS面向切面编程
    3. 哪些bug会导致崩溃,如何防护崩溃
    4. 怎么监控崩溃
    5. app的启动过程(考察LLVM编译过程、静态链接、动态链接、runtime初始化)
    6. 沙盒目录的每个文件夹划分的作用
    7. 简述下match-o文件结构

    系统基础知识

    1. 进程和线程的区别
    2. HTTPS的握手过程
    3. 什么是中间人攻击?怎么预防
    4. TCP的握手过程?为什么进行三次握手,四次挥手
    5. 堆和栈区的区别?谁的占用内存空间大
    6. 加密算法:对称加密算法和非对称加密算法区别
    7. 常见的对称加密和非对称加密算法有哪些
    8. MD5、Sha1、Sha256区别
    9. charles抓包过程?不使用charles4G网络如何抓包

    数据结构与算法

    对于移动开发者来说,一般不会遇到非常难的算法,大多以数据结构为主,笔者列出一些必会的算法,当然有时间了可以去LeetCode上刷刷题

    1. 八大排序算法
    2. 栈&队列
    3. 字符串处理
    4. 链表
    5. 二叉树相关操作
    6. 深搜广搜
    7. 基本的动态规划题、贪心算法、二分查找

    相关文章

      网友评论

        本文标题:阿里、字节 一套高效的iOS面试题解答(持续更新)

        本文链接:https://www.haomeiwen.com/subject/oxfwehtx.html