NSNotificationCenter

本文详细介绍了NSNotification和NSNotificationCenter的使用方法,包括如何创建和发送通知、如何注册和接收通知,以及NSNotification的各种属性和方法。同时,还讲解了NSNotificationCenter的单例模式、消息广播机制及其在解耦应用程序中的应用。

一.NSNotificationCenter的定义

@interface NSNotificationCenter : NSObject {
    @package
    void * __strong _impl;
    void * __strong _callback;
    void *_pad[11];
}

//单例获得消息中心对象
+ (NSNotificationCenter *)defaultCenter;

//增加消息监听 第一个参数是观察者为本身,第二个参数表示消息回调的方法,第三个消息通知的名字,第四个为notificationSender,nil表示表示接受所有发送者的消息
- (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullable NSString *)aName object:(nullable id)anObject;

//发送通知 三种方式
- (void)postNotification:(NSNotification *)notification;,
- (void)postNotificationName:(NSString *)aName object:(nullable id)anObject;
- (void)postNotificationName:(NSString *)aName object:(nullable id)anObject userInfo:(nullable NSDictionary *)aUserInfo;

//移除监听
- (void)removeObserver:(id)observer;
//移除监听特定消息
- (void)removeObserver:(id)observer name:(nullable NSString *)aName object:(nullable id)anObject;

//增加监听 又提供了一个以block方式实现的添加观察者的方法
- (id <NSObject>)addObserverForName:(nullable NSString *)name object:(nullable id)obj queue:(nullable NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))block NS_AVAILABLE(10_6, 4_0);

@end
每一个程序都有一个自己的通知中心,即 NSNotificationCenter对象。NSNotificationCenter这个也是我们平常用到的相关消息内容操作;NSNotificationCenter是一个单列,我们可以通过defaultCenter来获取到通知中心这个单列;通知中心实际上是iOS程序内部之间的一种消息广播机制,主要为了解决应用程序内部不同对象之间解耦而设计。它是基于观察者模式设计的,不能跨应用程序进程通信,当通知中心接收到消息之后会根据内部的消息转发表,将消息发送给订阅者;

知识点1:消息的运用步骤

1:创建通知并发送

 //这里的object代表通知的发送者
NSNotification *notification =[NSNotification notificationWithName:@"qjwallet" object:nil userInfo:nil];
        //通过通知中心发送通知
        [[NSNotificationCenter defaultCenter] postNotification:notification];

2:在接收的页面注册消息通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(qjwalletNotification:) name:@"qjwallet" object:nil];

在此函数中需要注意的内容:

  • notificationObserver不能为nil。
  • notificationSelector回调方法有且只有一个参数(NSNotification对象)。
  • 如果notificationName为nil,则会接收所有的通知(如果notificationSender不为空,则接收所有来自于notificationSender的所有通知)。
  • 如果notificationSender为nil,则会接收所有notificationName定义的通知;否则,接收由notificationSender发送的通知。
  • 监听同一条通知的多个观察者,在通知到达时,它们执行回调的顺序是不确定的,所以我们不能去假设操作的执行会按照添加观察者的顺序来执行。

3:处理接收到的内容,并进行操作

- (void)qjwalletNotification:(NSNotification *)text{
    //NSLog(@"%@",text.userInfo[@"orderid"]);
    NSLog(@"-----接收到通知------");
}
4:移除消息通知
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"qjwallet" object:nil];
}


知识点 2:关于创建通知并发送另外两个方法
方法1:[[NSNotificationCenter defaultCenter] postNotificationName:@"First" object:@"博客园"]; 

方法2:
NSDictionary *dict=[[NSDictionary alloc]initWithObjects:@[@"keso"] forKeys:@[@"key"]]; 
[[NSNotificationCenter defaultCenter] postNotificationName:@"Second" object:@"http://www.cnblogs.com" userInfo:dict];

上面两个方法已经把NSNotification对象封装一层,所以只要传入相关的对象属性就可以;

知识点3通过NSNotificationCenter注册通知NSNotification,viewDidLoad中

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationFirst:) name:@"First" object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSecond:) name:@"Second" object:nil];
-(void)notificationFirst:(NSNotification *)notification{
    NSString  *name=[notification name];
    NSString  *object=[notification object];
    NSLog(@"名称:%@----对象:%@",name,object);
}


-(void)notificationSecond:(NSNotification *)notification{
    NSString  *name=[notification name];
    NSString  *object=[notification object];
    NSDictionary  *dict=[notification userInfo];
    NSLog(@"名称:%@----对象:%@",name,object);
    NSLog(@"获取的值:%@",[dict objectForKey:@"key"]);
}
知识点4: 移除通知消息
-(void)dealloc{
    NSLog(@"观察者销毁了");
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

也可以针对某一个进行移除:

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"First" object:nil];

知识点5:如果notificationName为nil,则会接收所有的通知(如果notificationSender不为空,则接收所有来自于notificationSender的所有通知)。如代码清单1所示。如果notificationSender为nil,则会接收所有notificationName定义的通知;否则,接收由notificationSender发送的通知。监听同一条通知的多个观察者,在通知到达时,它们执行回调的顺序是不确定的,所以我们不能去假设操作的执行会按照添加观察者的顺序来执行

知识点6addObserverForName监听消息处理跟addObserver的差别

在前面addObserver有介绍它的四个参数作用,分别指定了通知的观察者、处理通知的回调、通知名及通知的发送对象;而addObserverForName同样是监听消息处理,只是它并没有观察者,却多出一个队列跟一个block的处理;addObserverForName参数说明,name和obj为nil时的情形与前面一个方法addObserver是相同的。如果queue为nil,则消息是默认在post线程中同步处理,即通知的post与转发是在同一线程中;但如果我们指定了操作队列,情况就变得有点意思了,我们一会再讲。block块会被通知中心拷贝一份(执行copy操作),以在堆中维护一个block对象,直到观察者被从通知中心中移除。所以,应该特别注意在block中使用外部对象,避免出现对象的循环引用。如果一个给定的通知触发了多个观察者的block操作,则这些操作会在各自的Operation Queue中被并发执行。所以我们不能去假设操作的执行会按照添加观察者的顺序来执行。该方法会返回一个表示观察者的对象,记得在不用时释放这个对象。

实例在指定队列中接收通知:

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserverForName:TEST_NOTIFICATION object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {

        NSLog(@"receive thread = %@", [NSThread currentThread]);
    }];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
       
        NSLog(@"post thread = %@", [NSThread currentThread]);
        [[NSNotificationCenter defaultCenter] postNotificationName:TEST_NOTIFICATION object:nil];
    });
}

@end
在这里,我们在主线程里添加了一个观察者,并指定在主线程队列中去接收处理这个通知。然后我们在一个全局队列中 post了一个通知。我们来看下输出结果:
post thread = <NSThread: 0x7ffe0351f5f0>{number = 2, name = (null)}
receive thread = <NSThread: 0x7ffe03508b30>{number = 1, name = main}

可以看到,消息的post与接收处理并不是在同一个线程中。如上面所提到的,如果queue为nil,则消息是默认在post线程中同步处理;

二:关于NSNotification的定义

@interface NSNotification : NSObject <NSCopying, NSCoding>
//这个成员变量是这个消息对象的唯一标识,用于辨别消息对象
@property (readonly, copy) NSString *name;
// 这个成员变量定义一个对象,可以理解为针对某一个对象的消息,代表通知的发送者
@property (nullable, readonly, retain) id object;
//这个成员变量是一个字典,可以用其来进行传值
@property (nullable, readonly, copy) NSDictionary *userInfo;

// 初始化方法
- (instancetype)initWithName:(NSString *)name object:(nullable id)object userInfo:(nullable NSDictionary *)userInfo NS_AVAILABLE(10_6, 4_0) NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;

@end

NSNotification顾名思义就是通知的作用,一个对象通知另外一个对象,可以用来传递参数、通信等作用,与delegate的一对一不同,通知是一对多的。在一个对象中注册了通知,那么其他任意对象都可以来对这个对象发出通知。因为属性都是只读的,如果要创建消息时要用下面NSNotification (NSNotificationCreation)分类相应的方法进行初始化;

三:NSNotification (NSNotificationCreation)分类

@interface NSNotification (NSNotificationCreation)

+ (instancetype)notificationWithName:(NSString *)aName object:(nullable id)anObject;
+ (instancetype)notificationWithName:(NSString *)aName object:(nullable id)anObject userInfo:(nullable NSDictionary *)aUserInfo;

- (instancetype)init /*NS_UNAVAILABLE*/;    /* do not invoke; not a valid initializer for this class */

@end
上面为初使化 NSNotification对象,用于消息的发送对象;

出自: 点击打开链接
参考: 点击打开链接
     点击打开链接
    
考虑柔性负荷的综合能源系统低碳经济优化调度【考虑碳交易机制】(Matlab代码实现)内容概要:本文围绕“考虑柔性负荷的综合能源系统低碳经济优化调度”展开,重点研究在碳交易机制下如何实现综合能源系统的低碳化与经济性协同优化。通过构建包含风电、光伏、储能、柔性负荷等多种能源形式的系统模型,结合碳交易成本与能源调度成本,提出优化调度策略,以降低碳排放并提升系统运行经济性。文中采用Matlab进行仿真代码实现,验证了所提模型在平衡能源供需、平抑可再生能源波动、引导柔性负荷参与调度等方面的有效性,为低碳能源系统的设计与运行提供了技术支撑。; 适合人群:具备一定电力系统、能源系统背景,熟悉Matlab编程,从事能源优化、低碳调度、综合能源系统等相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①研究碳交易机制对综合能源系统调度决策的影响;②实现柔性负荷在削峰填谷、促进可再生能源消纳中的作用;③掌握基于Matlab的能源系统建模与优化求解方法;④为实际综合能源项目提供低碳经济调度方案参考。; 阅读建议:建议读者结合Matlab代码深入理解模型构建与求解过程,重点关注目标函数设计、约束条件设置及碳交易成本的量化方式,可进一步扩展至多能互补、需求响应等场景进行二次开发与仿真验证。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值