小编Dan*_*agh的帖子

NSNotificationCenter将结构作为UserInfo的一部分传递

由于NSNotificationCenter.defaultCenter().postNotificationName userinfo只接受符合AnyObject协议的数据的字典,有没有人有任何建议如何发布结构作为NSNotification的一部分?

我最初的想法是将结构包装在一个类中 - 但是那么首先使用结构的意义何在.

我是否遗漏了某些内容,或者这只是将Swift与为Objective C构建的API混为一谈的结果?

这是我所描述的一个示范: -

class wrapper: NSObject {

  var aStructToWrap: aStruct

  init(theStruct: aStruct) {

    aStructToWrap = theStruct

    super.init()
  }

}

struct aStruct {
    var aValue: String
}

let aRealStruct = aStruct(aValue: "egg")


NSNotificationCenter.defaultCenter().postNotificationName("aKey", object: nil, userInfo: ["anotherKey": aRealStruct]) // ERR: Extra argument 'userinfo' in call

let wrappedStruct = wrapper(theStruct: aRealStruct)

NSNotificationCenter.defaultCenter().postNotificationName("aKey", object: nil, userInfo: ["anotherKey": wrappedStruct]) // no error
Run Code Online (Sandbox Code Playgroud)

struct nsnotification userinfo swift

18
推荐指数
1
解决办法
3934
查看次数

标签 统计

nsnotification ×1

struct ×1

swift ×1

userinfo ×1