2

I have found a line in my code that doesn't like to be run on iOS8, but have a way to perform the same task on iOS8 with different logic, that doesn't like iOS9.

I have used if #available(iOS 9, *) to perform the code I need on iOS9 and code on iOS8. However, when running, after a few calls to the function, the iOS9 device runs the code it shouldn't and crashes. Did I miss step in setting up the if #available(iOS 9, *)?

The code is below

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)
}
Bista
  • 7,869
  • 3
  • 27
  • 55
Mark
  • 148
  • 3
  • 14

2 Answers2

2
   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)
    }
Rahul Patel
  • 1,822
  • 12
  • 21
0

The available check should be working. Since the code to be exdcuted is equal in both cases (apart from the if), the problem is likely to be that the if condition (self.otherChats[loc].last?.timestamp! != messageObj.timestamp!) is true, when you think it should be false.

Try to add logs to both #available blocks, and most likely you'll see, that the second block (the iOS 8 one) is never executed on an iOS 9 device.

FreeNickname
  • 7,398
  • 2
  • 30
  • 60
  • if `(self.otherChats[loc].last?.timestamp! != messageObj.timestamp!)` was true or false, it should never run past the `else` since the `else` is linked with `if #available(iOS 9, *) { ` – Mark Jan 19 '16 at 21:35
  • @MarkMasterson, that's exactly what I'm trying to say. Are you sure that is was in the `#available` else block? Because the result is the same in both. Did you put log/brakepoint there? If it is, then it's probably a bug, and it's better to report it to Apple via http://bugreport.apple.com. – FreeNickname Jan 19 '16 at 21:56