Object Oriented Programming(面向對象編程) 這種思想把一切看爲對象,把現實中的事物及關係抽象成對象,使咱們將處理現實中的實際問題簡化爲處理各類對象之間的問題面試
面向對象的三大特性sql
Q:NSString 爲何用 copy 修飾?用 strong 會有問題麼?編程
A:保證其安全性設計模式
由於若是用 strong 修飾,在進行 setter 時,傳進來的是一個NSMutableString 也是能夠賦值的(這裏也有多態的體現),可是若是 NSMutableString 發生變化的話,因爲是 strong 修飾, NSString 的那個對象也會發生變化且無感知,這種風險就存在了,而若是用 copy 修飾,在 setter 時,若是傳進來的不可變對象 NSString,那麼 copy 爲淺拷貝,若是傳進來的是可變對象 NSMutableString,那麼 copy 爲深拷貝且 copy 出來的這個新對象變爲了避免可變的對象緩存
事件傳遞:當一個事件產生好比說一個點擊事件,首先就要尋找最佳響應者,配合下面這兩個方法,找到最佳響應者以後,UIApplication發送事件給UIWindow,UIWindow發送事件給最佳響應者安全
- (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event; // recursively calls -pointInside:withEvent:. point is in the receiver's coordinate system - (BOOL)pointInside:(CGPoint)point withEvent:(nullable UIEvent *)event; // default returns YES if point is in bounds 複製代碼
響應鏈:若是最佳響應者能夠處理這個事件,那就響應結束,若是不能響應事件則經過 nextResponder 傳遞給下一個響應者,若是傳遞到UIApplication仍是不能響應則丟棄該事件bash
當 FPS 低於 60 的時候,肉眼就會感到卡頓多線程
因此每一幀繪製的時間須要小於 1000ms / 60 ≈ 16.7ms 纔不會感受到卡頓併發
系統生成圖像信號是靠 CPU + GPU 共同工做的,因此優化的話也是從這兩方面入手考慮異步
在block中使用外部變量時,block會捕獲其變量,具體規則以下
使用場景:在blcok內部對捕獲的值進行賦值
Objective-C 擴展了 C 語言,並加入了面向對象特性和消息傳遞機制,而這個擴展的核心是一個用 C 和 編譯語言 寫的 Runtime 庫。它是 Objective-C 面向對象和動態機制的基石。
經過內部維護的事件循環來對事件、消息進行管理的一個對象 沒有消息的時候休眠以免資源佔用(用戶態轉爲內核態),有消息時當即被喚醒(內核態轉爲用戶態)
隊列:負責任務的調度 線程:負責任務的執行
// OC 中涉及的隊列
dispatch_queue_t _cocurrentQueue;
dispatch_queue_t _globalCocurrentQueue;
dispatch_queue_t _serialQueue;
dispatch_queue_t _mainQueue;
複製代碼
串行隊列
_serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
_mainQueue = dispatch_get_main_queue();
複製代碼
並行隊列
_cocurrentQueue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
_globalCocurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
複製代碼
系統提供的並行隊列優先級
#define DISPATCH_QUEUE_PRIORITY_HIGH 2
#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0
#define DISPATCH_QUEUE_PRIORITY_LOW (-2)
#define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN
複製代碼
{
dispatch_queue_t global_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, global_queue, ^{
dispatch_sync(global_queue, ^{
sleep(5);
NSLog(@"1 %@",[NSThread currentThread]);
});
dispatch_sync(global_queue, ^{
sleep(3);
NSLog(@"2 %@",[NSThread currentThread]);
});
dispatch_sync(global_queue, ^{
sleep(1);
NSLog(@"3 %@",[NSThread currentThread]);
});
});
dispatch_group_async(group, global_queue, ^{
NSLog(@"4 %@",[NSThread currentThread]);
});
dispatch_group_async(group, global_queue, ^{
NSLog(@"5 %@",[NSThread currentThread]);
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"三個任務都執行完畢後執行");
});
}
複製代碼
執行結果:
{
dispatch_sync(_globalCocurrentQueue, ^{
NSLog(@"登陸");
});
dispatch_async(_globalCocurrentQueue, ^{
NSLog(@"下載攻略");
});
dispatch_async(_globalCocurrentQueue, ^{
NSLog(@"下載音樂");
});
}
複製代碼
安全的:不引發 server 端任何狀態變化 冪等的:同一請求方法執行屢次與執行一次效果相同
空了繼續更
在簡書準備發佈呢,系統提示須要到 9.28 才能發,忽然想到還有個好平臺--掘金,就直接粘過來了😆