iOS 协议(protocol) 和通知(NSNotification)聊点深一点的

协议(protocol)

关于协议的一些定义,简单使用方法就不多说了.

协议可以添加哪些:

1.添加方法(类方法/属性方法)
2.添加属性

关于协议添加属性来举个例子:

#import <Foundation/Foundation.h>
@protocol PersonProtocol <NSObject>
@property (nonatomic, strong) NSString *name;//协议中的属性
@end

@interface Person : NSObject
@property (nonatomic, weak) id<PersonProtocol> delegade;
@end

找个类实现协议

#import "Student.h"

@interface Student ()<PersonProtocol>
@end

@implementation Student
//协议里的属性
@synthesize name;

@end

应用以下:

Person *onePerson = [Person new];
        Student *xiaoming = [Student new];
        onePerson.delegade = xiaoming;
        onePerson.delegade.name = @"xiaoming";
        NSLog(@"%@",xiaoming.name);

打印结果如下

xiaoming

通知(NSNotification)

先来看下常用的发通知的套路
发送:

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"要发通知啦");
    [[NSNotificationCenter defaultCenter] postNotificationName:@"aaa" object:nil];
    NSLog(@"通知发完啦");
}

接收:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(testNoti) name:@"aaa" object:nil];
    return YES;
}

- (void)testNoti {
    NSLog(@"收到通知啦 %@",[NSThread currentThread]);
}

看下打印:

要发通知啦
 收到通知啦 <NSThread: 0x17406d880>{number = 1, name = main}
通知发完啦

总结:

通过postNotificationName:发送通知是同步的, 会卡住当前线程. 就是说, 发送通知后,会等到对方接收到通知并执行完收到通知的方法后才会接着往下走.

关于移除通知

iOS 9以后UIViewController 添加通知后可以不用移除, 系统会自动给移除.

异步发送通知

异步发送通知需要三步
1.创建通知(NSNotification)
2.创建通知队列(NSNotificationQueue)
3.将通知放入队列中执行

发送:

 NSLog(@"发送通知前");
//创建通知
    NSNotification *noti = [[NSNotification alloc] initWithName:@"aaa" object:nil userInfo:nil];
//将通知加入队列,并设置为NSPostASAP,当当前runloop完成之后立即post
    [[NSNotificationQueue defaultQueue] enqueueNotification:noti postingStyle:NSPostASAP];
    NSLog(@"发送通知后");

接收:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(testNoti) name:@"aaa" object:nil];
    return YES;
}

- (void)testNoti {
    NSLog(@"接收到通知了%@",[NSThread currentThread]);
}

打印:

 发送通知前
 发送通知后
 接收到通知了<NSThread: 0x282bb2e40>{number = 1, name = main}

将通知加入到队列的方法有个枚举参数:

typedef NS_ENUM(NSUInteger, NSPostingStyle) {
    NSPostWhenIdle = 1,      // 当runloop处于空闲状态时post
    NSPostASAP = 2,    // 当当前runloop完成之后立即post
    NSPostNow = 3    // 立即post,同步(会立即执行)
};

指定枚举参数后, 队列会根据type, 在合适的时机将NSNotification 发送到NSNotificationCenter,这就达到了异步执行的效果.

NSPostWhenIdleNSPostASAP差不多, 都会异步执行,NSPostNow会立即执行

通知合并

将通知放入队列执行还有另外一个方法:
- (void)enqueueNotification:(NSNotification *)notification postingStyle:(NSPostingStyle)postingStyle coalesceMask:(NSNotificationCoalescing)coalesceMask forModes:(nullable NSArray<NSRunLoopMode> *)modes;
方法的第二个参数:(NSNotificationCoalescing)coalesceMask就是控制通知的合并策略.

如下:

typedef NS_OPTIONS(NSUInteger, NSNotificationCoalescing) {
    NSNotificationNoCoalescing = 0,  // 不合成
    NSNotificationCoalescingOnName = 1,  // 根据NSNotification的name字段进行合成
    NSNotificationCoalescingOnSender = 2  // 根据NSNotification的object字段进行合成
};

发送:

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"发送通知前");
    NSNotification *noti = [[NSNotification alloc] initWithName:@"aaa" object:nil userInfo:nil];
    [[NSNotificationQueue defaultQueue] enqueueNotification:noti postingStyle:NSPostASAP];
    [[NSNotificationQueue defaultQueue] enqueueNotification:noti postingStyle:NSPostWhenIdle];
    [[NSNotificationQueue defaultQueue] enqueueNotification:noti postingStyle:NSPostWhenIdle coalesceMask:NSNotificationCoalescingOnName forModes:nil];
    NSLog(@"发送通知后");
}

接收:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(testNoti) name:@"aaa" object:nil];
    return YES;
}

- (void)testNoti {
    NSLog(@"接收到通知了%@",[NSThread currentThread]);
}

打印:

发送通知前
发送通知后
接收到通知了<NSThread: 0x280fbee40>{number = 1, name = main}

注意, 如果直接使用postNotificationName:或者指定NSPostNow提交方式或者指定合并策略为NSNotificationNoCoalescing 时,不会合并通知

参考:
http://www.cocoachina.com/ios/20170426/19124.html

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,657评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,889评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,057评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,509评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,562评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,443评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,251评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,129评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,561评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,779评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,902评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,621评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,220评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,838评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,971评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,025评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,843评论 2 354

推荐阅读更多精彩内容

  • 1.ios高性能编程 (1).内层 最小的内层平均值和峰值(2).耗电量 高效的算法和数据结构(3).初始化时...
    欧辰_OSR阅读 29,385评论 8 265
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,101评论 1 32
  • OC语言基础 1.类与对象 类方法 OC的类方法只有2种:静态方法和实例方法两种 在OC中,只要方法声明在@int...
    奇异果好补阅读 4,271评论 0 11
  • 1.KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受...
    BEYOND黄阅读 1,542评论 0 6
  • 能聊的来就聊,不想聊就不聊,咱们能不能不尬聊! 正常来讲,聊天对很多人来说应该是一件简单的事,两个人相见随便说点什...
    知更喵阅读 1,042评论 0 1