比较 Objective-C 中的版本号
我正在编写一个接收带有项目和版本号的数据的应用程序。数字的格式类似于“1.0.1”或“1.2.5”。如何比较这些版本号?我认为它们必须首先格式化为字符串,不是吗?我必须使用哪些选项来确定“1.2.5”位于“1.0.1”之后?
I am writing an application that receives data with items and version numbers. The numbers are formatted like "1.0.1" or "1.2.5". How can I compare these version numbers? I think they have to be formatted as a string first, no? What options do I have to determine that "1.2.5" comes after "1.0.1"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
这是比较版本的最简单方法,请记住“1”<<。 “1.0”< “1.0.0”:
This is the simplest way to compare versions, keeping in mind that "1" < "1.0" < "1.0.0":
我将添加我的方法,该方法将严格的数字版本(无 a、b、RC 等)与任意数量的组件进行比较。
I'll add my method, which compares strictly numeric versions (no a, b, RC etc.) with any number of components.
这是 Nathan de Vries 答案的扩展,旨在解决 1 << 问题。 1.0< 1.0.0 等。
首先,我们可以使用
NSString
类别解决版本字符串上额外的“.0”问题:使用上面的
NSString
类别,我们可以缩短版本号以删除不必要的 .0现在我们仍然可以使用 Nathan de Vries 提出的精美简单的方法:
This is an expansion to Nathan de Vries answer to address the problem of 1 < 1.0 < 1.0.0 etc.
First off we can address the problem of extra ".0"'s on our version string with an
NSString
category:With the above
NSString
category we can shorten our version numbers to drop the unnecessary .0'sNow we can still use the beautifully simple approach proposed by Nathan de Vries:
我自己做的,使用类别..
来源..
测试..
I made it myself,use Category..
Source..
Test..
Sparkle(MacOS 上最流行的软件更新框架)有一个 Sparkle(MacOS 上最流行的软件更新框架)。 com/sparkle-project/Sparkle/blob/3a5c620b60f483b71f8c28573ac29bf85fda6193/Sparkle/SUStandardVersionComparator.m" rel="nofollow noreferrer">SUStandardVersionComparator 类执行此操作,并且还考虑内部版本号和 beta 标记。即它正确地比较
1.0.5 > 1.0.5b7
或2.0 (2345)> 2.0(2100)。该代码仅使用 Foundation,因此在 iOS 上也应该可以正常工作。
Sparkle (the most popular software update framework for MacOS) has a SUStandardVersionComparator class that does this, and also takes into account build numbers and beta markers. I.e. it correctly compares
1.0.5 > 1.0.5b7
or2.0 (2345) > 2.0 (2100)
. The code only uses Foundation, so should work fine on iOS as well.查看我在 github 上实现简单版本检查的 NSString 类别; https://github.com/stijnster/NSString-compareToVersion
这将返回一个 NSComparisonResult 这比使用更准确;
还添加了助手;
Check out my NSString category that implements easy version checking on github; https://github.com/stijnster/NSString-compareToVersion
This will return a NSComparisonResult which is more accurate then using;
Helpers are also added;
Swift 2.2 版本:
Swift 3 版本:
Swift 2.2 Version :
Swift 3 Version :
我想我只是分享一个我为此整理的函数。它一点也不完美。请看一下示例和结果。但是,如果您正在检查自己的版本号(我必须这样做来管理数据库迁移等事情),那么这可能会有所帮助。
(当然,还要删除方法中的日志语句。这些语句可以帮助您了解它的全部功能)
测试:
结果:
注意 alpha 有效,但是你必须非常小心。一旦你在某个时刻进入 alpha,你就无法通过更改其后面的任何其他次要数字来扩展它。
代码:
I thought I'd just share a function I pulled together for this. It is not perfect at all. Please take a look that the examples and results. But if you are checking your own version numbers (which I have to do to manage things like database migrations) then this may help a little.
(also, remove the log statements in the method, of course. those are there to help you see what it does is all)
Tests:
Results:
notice that alpha works but you have to be very careful with it. once you go alpha at some point you cannot extend that by changing any other minor numbers behind it.
Code:
我的 iOS 库 AppUpdateTracker 包含一个 NSString 类别 来执行这种比较。 (实现基于 DonnaLea 的回答。)
用法如下:
此外,您可以使用它来跟踪您的应用程序的安装/更新状态:
My iOS library AppUpdateTracker contains an NSString category to perform this sort of comparison. (Implementation is based off DonnaLea's answer.)
Usage would be as follows:
Additionally, you can use it to keep track of your app's installation/update status:
Glibc 有一个函数
strverscmp
和versionsort
…不幸的是,不能移植到iPhone,但你可以编写自己的相当容易。这种(未经测试的)重新实现仅来自阅读记录的行为,而不是阅读 Glibc 的源代码。Glibc has a function
strverscmp
andversionsort
… unfortunately, not portable to the iPhone, but you can write your own fairly easily. This (untested) re-implementation comes from just reading the documented behavior, and not from reading Glibc's source code.如果您知道每个版本号恰好有 3 个由点分隔的整数,您可以解析它们(例如使用
sscanf(3)
) 并比较它们:If you know each version number will have exactly 3 integers separated by dots, you can parse them (e.g. using
sscanf(3)
) and compare them:要快速检查版本,您可以使用以下命令
希望它可能会有所帮助。
To check the version in swift you can use following
Hope it might be helpful.
这是一个递归函数,可以处理任意长度的多个版本格式。它也适用于@“1.0”和@“1.0.0”
测试样本:
Here is a recursive function that do the works with multiple version formatting of any length. It also works for @"1.0" and @"1.0.0"
Test samples :