Comparing version numbers

后端 未结 8 1598
予麋鹿
予麋鹿 2020-12-11 11:39

Some time ago, I read that comparing version numbers can be done using the following code snippet:

NSString *vesrion_1 = @\"1.2.1\";
NSString *version_2 = @\         


        
8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-11 12:21

    I don't know of anything built in that will do it, but I have heard that the Sparkle framework has a version comparator.

    Browsing quickly through the source reveals that the SUStandardVersionComparator object seems to be in charge of it. It conforms to the protocol,which means you could probably just use it like this:

    NSString *versionA = @"1.2.1";
    NSString *versionB = @"1.2.0";
    id  comparator = [SUStandardVersionComparator defaultComparator];
    NSInteger result = [comparator compareVersion:versionA toVersion:versionB];
    if (result == NSOrderedSame) {
      //versionA == versionB
    } else if (result == NSOrderedAscending) {
      //versionA < versionB
    } else {
      //versionA > versionB
    }
    

    (note: code untested and typed in a browser. Caveat Implementor)

提交回复
热议问题