Sending , Listening for and Reacting to Notifications (发送,监听通知)

本文介绍了一个iOS应用中如何使用通知机制来实现不同组件间的通信。通过AppDelegate向Person类发送包含姓名信息的通知,Person类通过监听特定的通知并在接收到通知时更新其属性。

发送方:

//初始化通知内容  senderObject:self   userInfo:接收到通知时可从中获取到的信息

+ (instancetype)notificationWithName:(NSString *)aName object:(id)senderObject userInfo:(NSDictionary *)userInfo

//发送通知

- (void)postNotification:(NSNotification *)notification


接收方(观察者,监听者):

//注册通知

- (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender

//取消通知

- (void)removeObserver:(id)notificationObserver


e.g.

======AppDelegate.h文件======

#import <UIKit/UIKit.h>


/*extern:声明全局变量(不初始化),外部类可访问到 */

//notificationName

extern NSString *const kSetPersonInfoNotification; 

//The first-name key in the user-info dictionary of our notification 

extern NSString *const kSetPersonInfoKeyFirstName; 

// The last-name key in the user-info dictionary of our notification 

extern NSString *const kSetPersonInfoKeyLastName;


@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (nonatomic, strong) UIWindow *window;

@end

=======AppDelegate.m文件=====

#import "AppDelegate.h"

#import "Person.h"


NSString *const kSetPersonInfoNotification = @"SetPersonInfoNotification";

NSString *const kSetPersonInfoKeyFirstName = @"firstName";

NSString *const kSetPersonInfoKeyLastName = @"lastName";


@implementation AppDelegate

- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

    Person *steveJobs = [[Person alloc] init];   

    NSNotification *notification =

        [NSNotification  notificationWithName:kSetPersonInfoNotification

                                       object:self  //sender

                                     userInfo:@{kSetPersonInfoKeyFirstName : @"Steve",

                                                 kSetPersonInfoKeyLastName : @"Jobs"}];

    

    /* 在postNotification后,person class监听到并从notification中的userInfo中获取到了相关信息 */

   [[NSNotificationCenter defaultCenter] postNotification:notification];

    NSLog(@"Person's first name = %@", steveJobs.firstName);

    NSLog(@"Person's last name = %@", steveJobs.lastName);

    

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    return YES;

}

========Person.h=========

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property (nonatomic, copy) NSString *firstName;

@property (nonatomic, copy) NSString *lastName;

@end

========Person.m=========

#import "Person.h"

#import "AppDelegate.h"

@implementation Person

- (void) handleSetPersonInfoNotification:(NSNotification *)paramNotification{

    self.firstName =paramNotification.userInfo[kSetPersonInfoKeyFirstName];

    self.lastName = paramNotification.userInfo[kSetPersonInfoKeyLastName];   

}

//instancetype:返回类型  id:作为参数

- (instancetype) init{

    self = [super init];

    if (self != nil){       

       NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

        //注册通知  addObserver必须要对应一个removeObserver,否则可能会造成内存泄漏     

        [center addObserver:self  //观察者,监听者

                  selector:@selector(handleSetPersonInfoNotification:) //接收到通知时,执行的方法,必须要有一个NSNotification参数

                      name:kSetPersonInfoNotification   //通知名称

                    object:[[UIApplication sharedApplication] delegate]];  //发送(通知)者        

    }

    return self;

}

- (void) dealloc{

    //取消注册

   [[NSNotificationCenter defaultCenter] removeObserver:self];

}

@end



多源动态最优潮流的分布鲁棒优化方法(IEEE118节点)(Matlab代码实现)内容概要:本文介绍了基于Matlab代码实现的多源动态最优潮流的分布鲁棒优化方法,适用于IEEE118节点电力系统。该方法结合两阶段鲁棒模型与确定性模型,旨在应对电力系统中多源输入(如可再生能源)的不确定性,提升系统运行的安全性与经济性。文中详细阐述了分布鲁棒优化的建模思路,包括不确定性集合的构建、目标函数的设计以及约束条件的处理,并通过Matlab编程实现算法求解,提供了完整的仿真流程与结果分析。此外,文档还列举了大量相关电力系统优化研究案例,涵盖微电网调度、电动汽车集群并网、需求响应、储能配置等多个方向,展示了其在实际工程中的广泛应用价值。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的研究生、科研人员及从事能源系统优化工作的工程师。; 使用场景及目标:①用于研究高比例可再生能源接入背景下电力系统的动态最优潮流问题;②支撑科研工作中对分布鲁棒优化模型的复现与改进;③为电力系统调度、规划及运行决策提供理论支持与仿真工具。; 阅读建议:建议读者结合提供的Matlab代码与IEEE118节点系统参数进行实操演练,深入理解分布鲁棒优化的建模逻辑与求解过程,同时可参考文中提及的其他优化案例拓展研究思路。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值