Mark
Mark

Reputation: 158

if #available(iOS 9, *) Not Working

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)
}

Upvotes: 2

Views: 2677

Answers (2)

FreeNickname
FreeNickname

Reputation: 7764

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.

Upvotes: 0

Rahul Patel
Rahul Patel

Reputation: 1832

   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)
    }

Upvotes: 2

Related Questions