为什么这行NSNotificationCenter.defaultCenter()。postNotificationName(...)抛出SIGABRT

时间:2014-07-23 23:04:37

标签: swift nsnotificationcenter xcode6

我在swift中玩NSNotificationCenter。这是我在main.swift

中的代码

导入基金会

class T: NSObject {

    func someAction(notification: NSNotification) {
        println(notification.userInfo)
    }
}

var dataDict = Dictionary<String, String>()
dataDict["test"] = "test"
dataDict["test1"] = "test1"


var t = T();
NSNotificationCenter.defaultCenter().addObserver(t, selector:"someAction", name: "someAction", object:nil)



NSNotificationCenter.defaultCenter().postNotificationName("someAction", object:nil, userInfo:dataDict)

let runloop = NSRunLoop.currentRunLoop();
runloop.run();

println("Done");

postNotification行,我有一个例外:

enter image description here

这是一些堆栈跟踪:

2014-07-24 08:58:59.959 testswiftcli2[2171:303] -[_TtC13testswiftcli21T someAction]: unrecognized selector sent to instance 0x1005005a0
2014-07-24 08:58:59.961 testswiftcli2[2171:303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_TtC13testswiftcli21T someAction]: unrecognized selector sent to instance 0x1005005a0'

当我试图检查实例时,我得到了这个:

(lldb) po 0x1005005a0
4300211616

似乎变量已经超出了范围。这是一个正确的结论吗?

上面的代码有什么问题?

1 个答案:

答案 0 :(得分:6)

选择器应该是someAction:而不是someAction,因为它有一个参数。