版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2018.03.30 |
前言
iOS圈內(nèi)有幾個人大家基本都知道底洗,比如說王巍狸臣、唐巧蹋砚,還有YYKit框架的作者現(xiàn)任職于滴滴的郭曜源 - ibireme等聪铺。這里有一篇唐巧對他的專訪化焕,還有他的 GitHub - Yaoyuan 和 博客,這里貼出來框架YYKit 框架铃剔。接下來幾篇我們就一起來看一下這個框架撒桨。感興趣的可以看上面寫的幾篇。
1. YYKit源碼探究(一) —— 基本概覽
2. YYKit源碼探究(二) —— NSString分類之Hash(一)
3. YYKit源碼探究(三) —— NSString分類之Encode and decode(二)
4. YYKit源碼探究(四) —— NSString分類之Drawing(三)
5. YYKit源碼探究(五) —— NSString分類之Regular Expression(四)
6. YYKit源碼探究(六) —— NSString分類之NSNumber Compatible(五)
7. YYKit源碼探究(七) —— NSString分類之Utilities(六)
8. YYKit源碼探究(八) —— NSNumber分類(一)
9. YYKit源碼探究(九) —— UIFont分類之架構(gòu)分析和Font Traits(一)
10. YYKit源碼探究(十) —— UIFont分類之Create font(二)
11. YYKit源碼探究(十一) —— UIFont分類之Load and unload font(三)
12. YYKit源碼探究(十二) —— UIFont分類之Dump font data(四)
13. YYKit源碼探究(十三) —— UIImage分類之框架結(jié)構(gòu)和Create image部分(一)
14. YYKit源碼探究(十四) —— UIImage分類之Image Info(二)
15. YYKit源碼探究(十五) —— UIImage分類之Modify Image(三)
16. YYKit源碼探究(十六) —— UIImage分類之Image Effect(四)
17. YYKit源碼探究(十七) —— UIImageView分類之架構(gòu)和image部分(一)
18. YYKit源碼探究(十八) —— UIImageView分類之highlight image部分(二)
19. YYKit源碼探究(十九) —— UIScreen分類(一)
20. YYKit源碼探究(二十) —— UIScrollView分類(一)
21. YYKit源碼探究(二十一) —— UITableView分類(一)
22. YYKit源碼探究(二十二) —— UITextField分類(一)
23. YYKit源碼探究(二十三) —— UIView分類(一)
24. YYKit源碼探究(二十四) —— UIPasteboard分類(一)
25. YYKit源碼探究(二十五) —— UIGestureRecognizer分類(一)
26. YYKit源碼探究(二十六) —— UIDevice分類框架及Device Information(一)
27. YYKit源碼探究(二十七) —— UIDevice分類之Network Information(二)
28. YYKit源碼探究(二十八) —— UIDevice分類之Disk Space(三)
29. YYKit源碼探究(二十九) —— UIDevice分類之Memory Information(四)
30. YYKit源碼探究(三十) —— UIDevice分類之CPU Information(五)
31. YYKit源碼探究(三十一) —— UIControl分類(一)
32. YYKit源碼探究(三十二) —— UIColor分類之Create a UIColor Object(一)
33. YYKit源碼探究(三十三) —— UIColor分類之Get color's description(二)
34. YYKit源碼探究(三十四) —— UIColor分類之Retrieving Color Information(三)
35. YYKit源碼探究(三十五) —— UIButton分類之image(一)
36. YYKit源碼探究(三十六) —— UIButton分類之background image(二)
37. YYKit源碼探究(三十七) —— UIBezierPath分類(一)
38. YYKit源碼探究(三十八) —— UIBarButtonItem分類(一)
39. YYKit源碼探究(三十九) —— UIApplication分類(一)
40. YYKit源碼探究(四十) —— NSTimer分類(一)
41. YYKit源碼探究(四十一) —— NSParagraphStyle分類(一)
42. YYKit源碼探究(四十二) —— NSObject分類之YYModel(一)
43. YYKit源碼探究(四十三) —— NSObject分類之KVO(二)
44. YYKit源碼探究(四十四) —— NSObject分類之Sending messages with variable parameters(三)
45. YYKit源碼探究(四十五) —— NSObject分類之Swap method (Swizzling)(四)
46. YYKit源碼探究(四十六) —— NSObject分類之Associate value(五)
47. YYKit源碼探究(四十七) —— NSObject分類之Other(六)
回顧
上一篇主要介紹了NSObject
分類Others
部分键兜,這一篇主要看一下NSNotificationCenter
分類凤类。
API
下面我們就一起看一下API文檔。
/**
Provide some method for `NSNotificationCenter`
to post notification in different thread.
*/
@interface NSNotificationCenter (YYAdd)
/**
Posts a given notification to the receiver on main thread.
If current thread is main thread, the notification is posted synchronously;
otherwise, is posted asynchronously.
@param notification The notification to post.
An exception is raised if notification is nil.
*/
- (void)postNotificationOnMainThread:(NSNotification *)notification;
/**
Posts a given notification to the receiver on main thread.
@param notification The notification to post.
An exception is raised if notification is nil.
@param wait A Boolean that specifies whether the current thread blocks
until after the specified notification is posted on the
receiver on the main thread. Specify YES to block this
thread; otherwise, specify NO to have this method return
immediately.
*/
- (void)postNotificationOnMainThread:(NSNotification *)notification
waitUntilDone:(BOOL)wait;
/**
Creates a notification with a given name and sender and posts it to the
receiver on main thread. If current thread is main thread, the notification
is posted synchronously; otherwise, is posted asynchronously.
@param name The name of the notification.
@param object The object posting the notification.
*/
- (void)postNotificationOnMainThreadWithName:(NSString *)name
object:(nullable id)object;
/**
Creates a notification with a given name and sender and posts it to the
receiver on main thread. If current thread is main thread, the notification
is posted synchronously; otherwise, is posted asynchronously.
@param name The name of the notification.
@param object The object posting the notification.
@param userInfo Information about the the notification. May be nil.
*/
- (void)postNotificationOnMainThreadWithName:(NSString *)name
object:(nullable id)object
userInfo:(nullable NSDictionary *)userInfo;
/**
Creates a notification with a given name and sender and posts it to the
receiver on main thread.
@param name The name of the notification.
@param object The object posting the notification.
@param userInfo Information about the the notification. May be nil.
@param wait A Boolean that specifies whether the current thread blocks
until after the specified notification is posted on the
receiver on the main thread. Specify YES to block this
thread; otherwise, specify NO to have this method return
immediately.
*/
- (void)postNotificationOnMainThreadWithName:(NSString *)name
object:(nullable id)object
userInfo:(nullable NSDictionary *)userInfo
waitUntilDone:(BOOL)wait;
@end
下面我們一起看一下這個API
1. - (void)postNotificationOnMainThread:(NSNotification *)notification;
該方法的作用就是在主線程中發(fā)送通知普气,如果當前線程是主線程谜疤,那么就同步發(fā)送通知,如果當前線程不是主線程,那么就異步發(fā)送通知夷磕。
方法實現(xiàn)
- (void)postNotificationOnMainThread:(NSNotification *)notification {
if (pthread_main_np()) return [self postNotification:notification];
[self postNotificationOnMainThread:notification waitUntilDone:NO];
}
- (void)postNotificationOnMainThread:(NSNotification *)notification waitUntilDone:(BOOL)wait {
if (pthread_main_np()) return [self postNotification:notification];
[[self class] performSelectorOnMainThread:@selector(_yy_postNotification:) withObject:notification waitUntilDone:wait];
}
+ (void)_yy_postNotification:(NSNotification *)notification {
[[self defaultCenter] postNotification:notification];
}
2. - (void)postNotificationOnMainThread:(NSNotification *)notification waitUntilDone:(BOOL)wait;
該方法的作用就是在主線程中發(fā)送通知履肃,如果notification為nil就會拋出異常。
-
wait
:一個布爾值坐桩,指定當前線程是否阻塞尺棋,直到指定通知在主線程的receiver
上發(fā)布為止。 指定YES阻塞此線程; 否則撕攒,指定NO以便立即返回此方法陡鹃。
方法實現(xiàn)
- (void)postNotificationOnMainThread:(NSNotification *)notification waitUntilDone:(BOOL)wait {
if (pthread_main_np()) return [self postNotification:notification];
[[self class] performSelectorOnMainThread:@selector(_yy_postNotification:) withObject:notification waitUntilDone:wait];
}
3. - (void)postNotificationOnMainThreadWithName:(NSString *)name object:(nullable id)object;
使用給定名稱和sender創(chuàng)建通知,并將其通過主線程發(fā)送給receiver抖坪。 如果當前線程是主線程萍鲸,則通知被同步發(fā)布; 否則,將異步發(fā)布擦俐。
方法實現(xiàn)
- (void)postNotificationOnMainThreadWithName:(NSString *)name object:(id)object {
if (pthread_main_np()) return [self postNotificationName:name object:object userInfo:nil];
[self postNotificationOnMainThreadWithName:name object:object userInfo:nil waitUntilDone:NO];
}
- (void)postNotificationOnMainThreadWithName:(NSString *)name object:(id)object userInfo:(NSDictionary *)userInfo waitUntilDone:(BOOL)wait {
if (pthread_main_np()) return [self postNotificationName:name object:object userInfo:userInfo];
NSMutableDictionary *info = [[NSMutableDictionary allocWithZone:nil] initWithCapacity:3];
if (name) [info setObject:name forKey:@"name"];
if (object) [info setObject:object forKey:@"object"];
if (userInfo) [info setObject:userInfo forKey:@"userInfo"];
[[self class] performSelectorOnMainThread:@selector(_yy_postNotificationName:) withObject:info waitUntilDone:wait];
}
4. - (void)postNotificationOnMainThreadWithName:(NSString *)name object:(nullable id)object userInfo:(nullable NSDictionary *)userInfo;
使用給定名稱和sender創(chuàng)建通知脊阴,并將其通過主線程發(fā)送給receiver。 如果當前線程是主線程蚯瞧,則通知被同步發(fā)布; 否則嘿期,將異步發(fā)布。
方法實現(xiàn)
- (void)postNotificationOnMainThreadWithName:(NSString *)name object:(id)object userInfo:(NSDictionary *)userInfo {
if (pthread_main_np()) return [self postNotificationName:name object:object userInfo:userInfo];
[self postNotificationOnMainThreadWithName:name object:object userInfo:userInfo waitUntilDone:NO];
}
- (void)postNotificationOnMainThreadWithName:(NSString *)name object:(id)object userInfo:(NSDictionary *)userInfo waitUntilDone:(BOOL)wait {
if (pthread_main_np()) return [self postNotificationName:name object:object userInfo:userInfo];
NSMutableDictionary *info = [[NSMutableDictionary allocWithZone:nil] initWithCapacity:3];
if (name) [info setObject:name forKey:@"name"];
if (object) [info setObject:object forKey:@"object"];
if (userInfo) [info setObject:userInfo forKey:@"userInfo"];
[[self class] performSelectorOnMainThread:@selector(_yy_postNotificationName:) withObject:info waitUntilDone:wait];
}
5. - (void)postNotificationOnMainThreadWithName:(NSString *)name object:(nullable id)object userInfo:(nullable NSDictionary *)userInfo waitUntilDone:(BOOL)wait;
在主線程中發(fā)送通知并發(fā)出埋合。
方法實現(xiàn)
- (void)postNotificationOnMainThreadWithName:(NSString *)name object:(id)object userInfo:(NSDictionary *)userInfo waitUntilDone:(BOOL)wait {
if (pthread_main_np()) return [self postNotificationName:name object:object userInfo:userInfo];
NSMutableDictionary *info = [[NSMutableDictionary allocWithZone:nil] initWithCapacity:3];
if (name) [info setObject:name forKey:@"name"];
if (object) [info setObject:object forKey:@"object"];
if (userInfo) [info setObject:userInfo forKey:@"userInfo"];
[[self class] performSelectorOnMainThread:@selector(_yy_postNotificationName:) withObject:info waitUntilDone:wait];
}
后記
本篇主要講述了
NSNotificationCenter
分類备徐,感興趣的給個贊或者關(guān)注~~~~