Swift - 无法识别的选择器发送到实例

时间:2015-08-25 17:33:18

标签: ios swift nsnotificationcenter nsnotifications

我在Swift项目上设置了一个简单的NSNotification

NSNotificationCenter.defaultCenter().addObserver(self, selector: "serviceAccessChanged", name:"LocationAccessChangedNotification", object: nil)

我也试过......

NSNotificationCenter.defaultCenter().addObserver(self, selector: "serviceAccessChanged:", name:"LocationAccessChangedNotification", object: nil)

调用的方法看起来像这样。

private func serviceAccessChanged() {
    println("serviceAccessChanged")
}

发出通知时,我收到以下错误。

-[CoolApp.HomeViewController serviceAccessChanged]: unrecognized selector sent to instance 0x7fc91324bba0

出了什么问题,如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

私有函数不会暴露给Objective C,这就是你得到这个异常的原因。使此方法可访问,并使用serviceAccessChanged选择器。

答案 1 :(得分:1)

只需使用@objc

标记您的私有方法即可
String sql = "SELECT *" +
              " FROM " + tabla +
          " ORDER BY fechatotal DESC";
try (FileWriter writer = new FileWriter(file)) {
    writer.write("fecha" +
                ",hora" +
                ",fechatotal" +
                ",temperatura" +
                ",humedad" +
                ",luminosidad");
    try (ResultSet resultado = st.executeQuery(sql)) {
        writer.write("\n" + resultado.getDate("fecha") +
                      "," + resultado.getTime("hora") +
                      "," + resultado.getTimestamp("fechatotal") +
                      "," + resultado.getDouble("temperatura") +
                      "," + resultado.getDouble("humedad") +
                      "," + resultado.getDouble("luminosidad"));
    }
}