版本记录
版本号 | 时间 |
---|---|
V1.0 | 2018.03.30 |
前言
iOS圈内有几个人大家基本都知道,比如说王巍、唐巧,还有YYKit框架的作者现任职于滴滴的郭曜源 - 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分类之架构分析和Font Traits(一)
10. YYKit源码探究(十) —— UIFont分类之Create font(二)
11. YYKit源码探究(十一) —— UIFont分类之Load and unload font(三)
12. YYKit源码探究(十二) —— UIFont分类之Dump font data(四)
13. YYKit源码探究(十三) —— UIImage分类之框架结构和Create image部分(一)
14. YYKit源码探究(十四) —— UIImage分类之Image Info(二)
15. YYKit源码探究(十五) —— UIImage分类之Modify Image(三)
16. YYKit源码探究(十六) —— UIImage分类之Image Effect(四)
17. YYKit源码探究(十七) —— UIImageView分类之架构和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;
该方法的作用就是在主线程中发送通知,如果当前线程是主线程,那么就同步发送通知,如果当前线程不是主线程,那么就异步发送通知。
方法实现
- (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;
该方法的作用就是在主线程中发送通知,如果notification为nil就会抛出异常。
-
wait
:一个布尔值,指定当前线程是否阻塞,直到指定通知在主线程的receiver
上发布为止。 指定YES阻塞此线程; 否则,指定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];
}
3. - (void)postNotificationOnMainThreadWithName:(NSString *)name object:(nullable id)object;
使用给定名称和sender创建通知,并将其通过主线程发送给receiver。 如果当前线程是主线程,则通知被同步发布; 否则,将异步发布。
方法实现
- (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创建通知,并将其通过主线程发送给receiver。 如果当前线程是主线程,则通知被同步发布; 否则,将异步发布。
方法实现
- (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;
在主线程中发送通知并发出。
方法实现
- (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
分类,感兴趣的给个赞或者关注~~~~