美文网首页iOS日常开发
知识点----持续整理

知识点----持续整理

作者: 风与鸾 | 来源:发表于2020-03-03 16:32 被阅读0次

    祝大家面试不迷茫

    一个整理比较好的面试集

    传送门

    iOS几种多线程的方式

    主要有三种:NSThread、NSoperationQueue、GCD

    GCD线程

     NSLog(@"1");
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        NSLog(@"2");
    
        [self performSelector:@selector(test) withObject:nil afterDelay:10];
        
        NSLog(@"3");
    });
    
    NSLog(@"4");
    
    - (void)test
    {
        
        NSLog(@"5");
    }
    
    /// 执行顺序12和4无序3
    ///test方式不执行
    /// 原因是如果是带afterDelay的延时函数,会在内部创建一个 NSTimer,然后添加到当前线程的RunLoop中。也就是如果当前线程没有开启RunLoop,该方法会失效。
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        NSLog(@"2");
    
        [self performSelector:@selector(test) withObject:nil afterDelay:10];
        /// RunLoop一定要在model item之后否则同样无效
        [[NSRunLoop currentRunLoop] run];
        NSLog(@"3");
    });
    
    Global Queue有哪几种优先级?
    可分为
    Default,Low,High
    DISPATCH_QUEUE_PRIORITY_BACKGROUND作何用?用于「I/O Throttle 」。
    
    官方文档:
    Items dispatched to the queue run at background priority; the queue is scheduled for execution after all high priority queues have been scheduled and the system runs items on a thread whose priority is set for background status. Such a thread has the lowest priority and any disk I/O is throttled to minimize the impact on the system.
    
    简而言之,对于重度磁盘I/O依赖的后台任务,如果对实时性要求不高,放到DISPATCH_QUEUE_PRIORITY_BACKGROUND Queue中是个好习惯,对系统更友好。
    
    实际上I/O Throttle还分为好几种,有Disk I/O Throttle,Memory I/O Throttle,和Network I/O Throttle。
    

    copy和mutableCopy的区别

    概念:
    There are two kinds of object copying: shallow copies and deep copies. The normal copy is a shallow copy that produces a new collection that shares ownership of the objects with the original. Deep copies create new objects from the originals and add those to the new collection
    
    注释:
    有两种类型的对象拷贝,浅拷贝和深拷贝。正常的拷贝,生成一个新的容器,但却是和原来的容器共用内部的元素,这叫做浅拷贝。深拷贝不仅生成新的容器,还生成了新的内部元素。
    
    比较容易混淆的一点:
    我相信大部分人认为copy就是shallow copies
    mutableCopy是deep copies。
    这种观点是错误,面试时特别,如果面试你和面试官持有不同意见,可以用官方注释来说明了。
    
    对于不可变或者可变的容器对象的mutableCopy或者copy操作都是浅拷贝!!!
    如果面试官用NSString来和你讨论浅深拷贝的区别,那你当心下是不是给你挖的坑,就看你灵性了。
    
    

    容器类型

    • copy操作返回的必然是一个不可变对象,无论源对象是可变对象还是不可变对象。如果源对象是一个不可变对象,那么它们(源对象和新生成的对象)指向同一个对象,如果源对象是可变对象,它们指向不同对象

    • mutableCopy返回的必然是一个可变对象,无论源对象是可变对象还是不可变对象,它们(源对象和新生成的对象)仍指向不同地址,是两个对象

    • copy和mutableCopy都生成新对象

    NSString

    @property (nonatomic, copy) NSString *string;
    
    NSMutableString *string1 = [[NSMutableString alloc] initWithString:@"222"];
    self.string = string1;
    [string1 appendString:@"copystring"];
    NSLog(@"string:%p+++++copy:%p",self.string,string1);
    NSLog(@"string:%@+++++copy:%@",self.string,string1);
    
    打印如下:
    2020-03-03 18:19:25.092379+0800 Bindo[13659:2296050] string:0x845b8387868c3717+++++copy:0x60000297bf60
    2020-03-03 18:19:25.092589+0800 Bindo[13659:2296050] string:222+++++copy:222copystring
    
    用copy关键字修饰时是两个不同的地址,修改互相不影响。
    
    @property (nonatomic, strong) NSString *string;
    打印如下:
    2020-03-03 18:22:30.522420+0800 Bindo[13678:2298117] string:0x600000236f70+++++copy:0x600000236f70
    2020-03-03 18:22:30.522672+0800 Bindo[13678:2298117] string:222copystring+++++copy:222copystring
    如果用strong关键字修饰时string和string1的地址一样,修改会影响另外一个对象。
    

    相关文章

      网友评论

        本文标题:知识点----持续整理

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