【问题标题】:if #available(iOS 9, *) Not Working如果 #available(iOS 9, *) 不工作
【发布时间】:2016-04-24 12:46:39
【问题描述】:

我在我的代码中发现了一行不喜欢在 iOS8 上运行,但有一种方法可以在 iOS8 上以不同的逻辑执行相同的任务,这与 iOS9 不同。

我已经使用if #available(iOS 9, *) 在 iOS9 上执行我需要的代码和在 iOS8 上执行代码。但是,在运行时,在对该函数进行几次调用后,iOS9 设备会运行它不应该运行的代码并崩溃。我是否错过了设置if #available(iOS 9, *) 的步骤?

代码如下

if #available(iOS 9, *) {
    if(self.otherChats[loc].last?.timestamp! != messageObj.timestamp!){
        self.otherChats[loc].append(messageObj)
        self.Notfication.postNotificationName(_Chat.Notification.DidGetMessage, object: nil)
    }
} else {
    self.otherChats[loc].append(messageObj)
    self.Notfication.postNotificationName(_Chat.Notification.DidGetMessage, object: nil)
}

【问题讨论】:

    标签: ios swift ios8 swift2 ios9


    【解决方案1】:
       let systemVersion = UIDevice.currentDevice().systemVersion        
        if((Double)(systemVersion) > 9.0)
        {
            if(self.otherChats[loc].last?.timestamp! != messageObj.timestamp!){
                self.otherChats[loc].append(messageObj)
                self.Notfication.postNotificationName(_Chat.Notification.DidGetMessage, object: nil)
            }
        }
        else
        {
            self.otherChats[loc].append(messageObj)
            self.Notfication.postNotificationName(_Chat.Notification.DidGetMessage, object: nil)
        }
    

    【讨论】:

      【解决方案2】:

      available 检查应该有效。由于在两种情况下要执行的代码都是相等的(除了if),问题很可能是if 条件(self.otherChats[loc].last?.timestamp! != messageObj.timestamp!)是true,而你认为​​它应该是@987654327 @。

      尝试将日志添加到两个 #available 块,您很可能会看到,第二个块(iOS 8 块)永远不会在 iOS 9 设备上执行。

      【讨论】:

      • 如果(self.otherChats[loc].last?.timestamp! != messageObj.timestamp!) 是真或假,它不应该越过else,因为elseif #available(iOS 9, *) { 链接
      • @MarkMasterson,这正是我想说的。你确定那是在#available else 块中吗?因为两者的结果是一样的。你把日志/刹车点放在那里了吗?如果是,那么它可能是一个错误,最好通过bugreport.apple.com向Apple报告。
      猜你喜欢
      • 2015-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多