如何在 Swift 的通知中发送枚举值?
How to send an enum value in a notification in Swift?
我想在通知中将枚举作为对象发送:
enum RuleError:String {
case Create, Update, Delete
}
class myClass {
func foo() {
NSNotificationCenter.defaultCenter().postNotificationName("RuleFailNotification",
object: RuleError.Create)
}
}
不幸的是,这不起作用,因为枚举 不匹配 AnyObject?
。
知道如何避免这个问题吗?
您正在使用的函数中的 object
参数是发送者,即发布通知的对象,而不是参数。查看文档 here.
您应该将要发送的枚举值作为参数放入用户信息字典中,并使用以下方法:
func postNotificationName(_ aName: String,
object anObject: AnyObject?,
userInfo aUserInfo: [NSObject : AnyObject]?)
你的情况:
let userInfo = ["RuleError" : RuleError.Create.rawValue]
NSNotificationCenter.defaultCenter().postNotificationName("RuleFailNotification",
object: self,
userInfo:userInfo)
要处理通知,请先注册:
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "handleRuleFailNotification:",
name: "RuleFailNotification",
object: nil)
然后处理:
func handleRuleFailNotification(notification: NSNotification) {
let userInfo = notification.userInfo
RuleError(rawValue: userInfo!["RuleError"] as! String)
}
最简单的解决方案是发送原始值
RuleError.Create.rawValue
稍后可以转换回枚举
RuleError(rawValue : "Create")
但是 object
参数不适合发送自定义数据。最好使用 userInfo
词典。
您可以使用装箱通过 NSNotification 发送纯 Swift 类型,如枚举。
final class Box<T>: NSObject {
let value: T
init(_ value: T) {
self.value = value
}
}
给post一个通知:
let userInfo = ["RuleError" : Box(RuleError.Create)]
NSNotificationCenter.defaultCenter().postNotificationName("RuleFailNotification",
object: self,
userInfo:userInfo)
在您的观察者中,该值可以检索为:
if let box = notification.userInfo?["RuleFailNotification"] as? Box<RuleError> {
let ruleError = box.value
}
您甚至可以发送带有关联值的 Swift 枚举。
例子。 Swift3
1: 创建通知名称
extension NSNotification.Name {
public static let NetworkReachabilityStatus: NSNotification.Name =
NSNotification.Name(rawValue: "NetworkReachabilityStatusChanged")
}
2:一些枚举
enum NetworkReachabilityStatus {
case connected
case notConnected
}
3:发布数据
NotificationCenter.default.post(name: Notification.Name.NetworkReachabilityStatus,
object:nil,
userInfo:[kNetworkRechabilityStatus: NetworkReachabilityStatus.notConnected])
- kNetworkRechabilityStatus - 是字符串常量
4:观察
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(networkStatusChanged(_:)), name: Notification.Name.NetworkReachabilityStatus, object: nil)
}
func networkStatusChanged(_ notification: Notification) {
let status: NetworkReachabilityStatus = notification.userInfo?[kNetworkRechabilityStatus] as! NetworkReachabilityStatus
switch status {
case .connected:
break
case .notConnected:
break
}
}
别忘了取消订阅通知:)
我想在通知中将枚举作为对象发送:
enum RuleError:String {
case Create, Update, Delete
}
class myClass {
func foo() {
NSNotificationCenter.defaultCenter().postNotificationName("RuleFailNotification",
object: RuleError.Create)
}
}
不幸的是,这不起作用,因为枚举 不匹配 AnyObject?
。
知道如何避免这个问题吗?
您正在使用的函数中的 object
参数是发送者,即发布通知的对象,而不是参数。查看文档 here.
您应该将要发送的枚举值作为参数放入用户信息字典中,并使用以下方法:
func postNotificationName(_ aName: String,
object anObject: AnyObject?,
userInfo aUserInfo: [NSObject : AnyObject]?)
你的情况:
let userInfo = ["RuleError" : RuleError.Create.rawValue]
NSNotificationCenter.defaultCenter().postNotificationName("RuleFailNotification",
object: self,
userInfo:userInfo)
要处理通知,请先注册:
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "handleRuleFailNotification:",
name: "RuleFailNotification",
object: nil)
然后处理:
func handleRuleFailNotification(notification: NSNotification) {
let userInfo = notification.userInfo
RuleError(rawValue: userInfo!["RuleError"] as! String)
}
最简单的解决方案是发送原始值
RuleError.Create.rawValue
稍后可以转换回枚举
RuleError(rawValue : "Create")
但是 object
参数不适合发送自定义数据。最好使用 userInfo
词典。
您可以使用装箱通过 NSNotification 发送纯 Swift 类型,如枚举。
final class Box<T>: NSObject {
let value: T
init(_ value: T) {
self.value = value
}
}
给post一个通知:
let userInfo = ["RuleError" : Box(RuleError.Create)]
NSNotificationCenter.defaultCenter().postNotificationName("RuleFailNotification",
object: self,
userInfo:userInfo)
在您的观察者中,该值可以检索为:
if let box = notification.userInfo?["RuleFailNotification"] as? Box<RuleError> {
let ruleError = box.value
}
您甚至可以发送带有关联值的 Swift 枚举。
例子。 Swift3
1: 创建通知名称
extension NSNotification.Name {
public static let NetworkReachabilityStatus: NSNotification.Name =
NSNotification.Name(rawValue: "NetworkReachabilityStatusChanged")
}
2:一些枚举
enum NetworkReachabilityStatus {
case connected
case notConnected
}
3:发布数据
NotificationCenter.default.post(name: Notification.Name.NetworkReachabilityStatus,
object:nil,
userInfo:[kNetworkRechabilityStatus: NetworkReachabilityStatus.notConnected])
- kNetworkRechabilityStatus - 是字符串常量
4:观察
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(networkStatusChanged(_:)), name: Notification.Name.NetworkReachabilityStatus, object: nil)
}
func networkStatusChanged(_ notification: Notification) {
let status: NetworkReachabilityStatus = notification.userInfo?[kNetworkRechabilityStatus] as! NetworkReachabilityStatus
switch status {
case .connected:
break
case .notConnected:
break
}
}
别忘了取消订阅通知:)