比较版本号 [英] Comparing version numbers

查看:128
本文介绍了比较版本号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前一段时间,我看到可以使用以下代码片段来比较版本号:

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 = @"1.2.0";

if ([version_1 compare:version_2 options:NSNumericSearch] == NSOrderedAscending) {
...
}

这似乎工作正常,但一个用户有一个问题,这是由于一个看似不正确的版本号比较。

This seems to work fine, but one user is having a problem, which is due to a seemingly incorrect version number comparison.

我的问题是,在所有情况下使用这种技术来比较版本号是否安全?

My question is, is it safe under all circumstances to use this technique to compare version numbers? Is it possible that the above comparison results in different outcomes on different machines?

推荐答案

我不知道任何事情内置即可,但我听说过 Sparkle框架有一个版本比较器。

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

通过源代码浏览可以显示 SUStandardVersionComparator 对象似乎负责。它符合 < SUVersionComparison> protocol ,这意味着你可能只是这样使用它:

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

NSString *versionA = @"1.2.1";
NSString *versionB = @"1.2.0";
id <SUVersionComparison> 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)

这篇关于比较版本号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆