Swift中Notification.Name这么难用怎么办
2018-03-11 00:00
531 查看
点击上方“程序员大咖”,选择“置顶公众号”关键时刻,第一时间送达!
以前的发送通知的参数就是一个简单的字符串:
NSNotificationCenter.defaultCenter().post("someStringThatShouldBeDeclared")
后来到了swift 3 中,改成了Notification.Name。定义在Notification的命名空间下,是一个结构体,初始化函数接收一个字符串。
extension NSNotification { public struct Name : RawRepresentable, Equatable, Hashable, Comparable {
public init(_ rawValue: String)
public init(rawValue: String) }}
用起来就麻烦了一点:
NotificationCenter.default.post(Notification.Name(rawValue: "MyNotificationName"))
如果还是按照以前的方式定义一个全局字符串常量就没有好好领会Swift精神了。
至少需要这样,通过extension声明一个静态的常量:
extension Notification.Name { static let AccountBalanceUpdated = Notification.Name("accountBalanceUpdated")} // invocationNotificationCenter.default.post(.AccountBalanceUpdated)
但是这种方式有一个小缺点,自定义的通知和系统的混在了一起,有时找起来比较尴尬。
这里其实也有另外一个问题,这种方式不能避免通知的名字重复。虽然如果命名规范不会有这样的问题,但是到底是个潜在的风险。
如果把上面两个问题合起来看,就有了另外一种方式:利用Enum。
先声明一个rawValue为字符串的枚举。为了规避命名的冲突,声明一个计算属性,在每个值的rawValue前插入一个字符串。再用这个字符串去生成NSNotification.Name:
enum CPNotification: String {
case userLogout
case userLogin
var stringValue: String {
return "CP" + rawValue
}
var notificationName: NSNotification.Name {
return NSNotification.Name(stringValue)
}
}
用起来就简单了,自己写一个扩展方法:
extension NotificationCenter {
static func post(customeNotification name: CPNotification, object: Any? = nil){
NotificationCenter.default.post(name: name.notificationName, object: object)
}
}
这样在使用时,直接点出来的就都是自定义的通知了。
当然在通知处理的地方也写个扩展方法用起来就更爽了,比如我用Rx所以这样写:
extension Reactive where Base: NotificationCenter {
func notification(custom name: CPNotification, object: AnyObject? = nil) -> Observable<Notification> {
return notification(name.notificationName, object: object)
}
}
用起来就是这样:
// 发送通知
NotificationCenter.post(customeNotification: .userLogout)
// 接收通知
let _ = NotificationCenter.default.rx.notification(custom: .userLogout).subscribe(onNext: { (value) in
CPNetworkConfig.userID = nil
})
作者:没故事的卓同学
链接:https://www.jianshu.com/p/105f6b133bd2
程序员大咖整理发布,转载请联系作者获得授权
[b]
【点击成为Java大神】[/b]
以前的发送通知的参数就是一个简单的字符串:
NSNotificationCenter.defaultCenter().post("someStringThatShouldBeDeclared")
后来到了swift 3 中,改成了Notification.Name。定义在Notification的命名空间下,是一个结构体,初始化函数接收一个字符串。
extension NSNotification { public struct Name : RawRepresentable, Equatable, Hashable, Comparable {
public init(_ rawValue: String)
public init(rawValue: String) }}
用起来就麻烦了一点:
NotificationCenter.default.post(Notification.Name(rawValue: "MyNotificationName"))
如果还是按照以前的方式定义一个全局字符串常量就没有好好领会Swift精神了。
至少需要这样,通过extension声明一个静态的常量:
extension Notification.Name { static let AccountBalanceUpdated = Notification.Name("accountBalanceUpdated")} // invocationNotificationCenter.default.post(.AccountBalanceUpdated)
但是这种方式有一个小缺点,自定义的通知和系统的混在了一起,有时找起来比较尴尬。
这里其实也有另外一个问题,这种方式不能避免通知的名字重复。虽然如果命名规范不会有这样的问题,但是到底是个潜在的风险。
如果把上面两个问题合起来看,就有了另外一种方式:利用Enum。
先声明一个rawValue为字符串的枚举。为了规避命名的冲突,声明一个计算属性,在每个值的rawValue前插入一个字符串。再用这个字符串去生成NSNotification.Name:
enum CPNotification: String {
case userLogout
case userLogin
var stringValue: String {
return "CP" + rawValue
}
var notificationName: NSNotification.Name {
return NSNotification.Name(stringValue)
}
}
用起来就简单了,自己写一个扩展方法:
extension NotificationCenter {
static func post(customeNotification name: CPNotification, object: Any? = nil){
NotificationCenter.default.post(name: name.notificationName, object: object)
}
}
这样在使用时,直接点出来的就都是自定义的通知了。
当然在通知处理的地方也写个扩展方法用起来就更爽了,比如我用Rx所以这样写:
extension Reactive where Base: NotificationCenter {
func notification(custom name: CPNotification, object: AnyObject? = nil) -> Observable<Notification> {
return notification(name.notificationName, object: object)
}
}
用起来就是这样:
// 发送通知
NotificationCenter.post(customeNotification: .userLogout)
// 接收通知
let _ = NotificationCenter.default.rx.notification(custom: .userLogout).subscribe(onNext: { (value) in
CPNetworkConfig.userID = nil
})
作者:没故事的卓同学
链接:https://www.jianshu.com/p/105f6b133bd2
程序员大咖整理发布,转载请联系作者获得授权
[b]
【点击成为Java大神】[/b]
相关文章推荐
- Swift中Notification.Name这么难用怎么办
- Swift中Notification.Name这么难用怎么办
- 通过Notification.Name看Swift是如何优雅的解决String硬编码
- fort minor 的remember the name 太酷了.怎么会有这么棒的歌.
- 博客是否应该这么弄呀?请前辈指点并教我今后该怎么更好?请教
- Linux搭建ftp在windows下这么访问,需要怎么设置【设置vsftp最大连接数和每个ip的最大连接数】
- 靠,论文怎么都这么罗嗦
- html中包含标签的name值含有.号时候怎么的取值!
- 现代地球人怎么这么怕热?
- swift 中怎么取得UIApplication的对象实例,并且利用appdelegate传值~
- 生活啊,怎么这么烦啊?
- swift 服务端要求json对象怎么办?(二)
- JOIN US框架-3(hibernate怎么变成这么好用了?)
- 怎么多了一个typename?
- Class a=new Class(){a=b,c=d}。C#语言什么意思 主要是实例化后怎么有大括号从来没这么用过。
- jquery.validate表单验证,特殊name怎么写rules
- 【别人的老师VS你的老师 】同样是老师,差别怎么这么大呢!?
- 概述史:五胡十六国过这么乱的历史怎么可能搞得懂?6000字短文帮您整个清清楚楚
- Swift还是Objective-C,你怎么选择
- swift没有了BlocksKit怎么办?