祝大家面試不迷茫
一個(gè)整理比較好的面試集
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");
}
/// 執(zhí)行順序12和4無序3
///test方式不執(zhí)行
/// 原因是如果是帶afterDelay的延時(shí)函數(shù),會(huì)在內(nèi)部創(chuàng)建一個(gè) NSTimer械念,然后添加到當(dāng)前線程的RunLoop中抖苦。也就是如果當(dāng)前線程沒有開啟RunLoop迁沫,該方法會(huì)失效诅炉。
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有哪幾種優(yōu)先級(jí)泞辐?
可分為
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.
簡(jiǎn)而言之咨演,對(duì)于重度磁盤I/O依賴的后臺(tái)任務(wù)闸昨,如果對(duì)實(shí)時(shí)性要求不高,放到DISPATCH_QUEUE_PRIORITY_BACKGROUND Queue中是個(gè)好習(xí)慣雪标,對(duì)系統(tǒng)更友好零院。
實(shí)際上I/O Throttle還分為好幾種溉跃,有Disk I/O Throttle村刨,Memory I/O Throttle,和Network I/O Throttle撰茎。
copy和mutableCopy的區(qū)別
概念:
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
注釋:
有兩種類型的對(duì)象拷貝嵌牺,淺拷貝和深拷貝。正常的拷貝龄糊,生成一個(gè)新的容器逆粹,但卻是和原來的容器共用內(nèi)部的元素,這叫做淺拷貝炫惩。深拷貝不僅生成新的容器僻弹,還生成了新的內(nèi)部元素。
比較容易混淆的一點(diǎn):
我相信大部分人認(rèn)為copy就是shallow copies
mutableCopy是deep copies他嚷。
這種觀點(diǎn)是錯(cuò)誤,面試時(shí)特別,如果面試你和面試官持有不同意見,可以用官方注釋來說明了蹋绽。
對(duì)于不可變或者可變的容器對(duì)象的mutableCopy或者copy操作都是淺拷貝!!!
如果面試官用NSString來和你討論淺深拷貝的區(qū)別芭毙,那你當(dāng)心下是不是給你挖的坑,就看你靈性了卸耘。
容器類型
copy操作返回的必然是一個(gè)不可變對(duì)象退敦,無論源對(duì)象是可變對(duì)象還是不可變對(duì)象。如果源對(duì)象是一個(gè)不可變對(duì)象蚣抗,那么它們(源對(duì)象和新生成的對(duì)象)指向同一個(gè)對(duì)象侈百,如果源對(duì)象是可變對(duì)象,它們指向不同對(duì)象
mutableCopy返回的必然是一個(gè)可變對(duì)象翰铡,無論源對(duì)象是可變對(duì)象還是不可變對(duì)象钝域,它們(源對(duì)象和新生成的對(duì)象)仍指向不同地址,是兩個(gè)對(duì)象
copy和mutableCopy都生成新對(duì)象
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關(guān)鍵字修飾時(shí)是兩個(gè)不同的地址两蟀,修改互相不影響网梢。
@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關(guān)鍵字修飾時(shí)string和string1的地址一樣,修改會(huì)影響另外一個(gè)對(duì)象赂毯。