Wongzigii Wongzigii - 2 months ago 37
Objective-C Question

Can not compare systemVersion in iOS 10

#define kIOS8_OR_LATER \
([[[UIDevice currentDevice] systemVersion] compare:@"8.0"] != NSOrderedAscending)


Why the comparison between iOS 8.0 and 10 not work?

if (kIOS8_OR_LATER) {
NSLog(@"1");
} else {
NSLog(@"0");
}


Device:


  • Simulator iOS 10 (14A345)

  • Real device iPhone 6s iOS 10.0.1 (14A403)



Xcode:


  • Version 8.0 (8A218a)



The console keeps logging 0, but works fine in iOS 9.

Answer

A normal string comparison looks at one character at a time. A 1 is ordered before an 8. So you need to make the comparison numeric:

#define kIOS8_OR_LATER \
    ([[[UIDevice currentDevice] systemVersion] compare:@"8.0" options:NSNumericSearch] != NSOrderedAscending)

Then the comparison is between 8 and 10.