将字符串与格式“2.0.1”、“2.0.09”进行比较
我需要获取用户的应用程序版本号并将其与服务器上当前的应用程序版本进行比较。如果用户的应用程序版本较低,那么他将收到一个弹出窗口来更新他的应用程序。在执行此操作时,我需要将应用程序的版本与可用版本进行比较。在 Objective-C 中,如何比较 "2.0.1"
和 "2.0.09"
格式的字符串并获得最高的字符串?
I need to get the user's app version number and compare it with the current app version on my server. If the user's app version is lower, then he will get a pop-up to update his app. While doing this, I need to compare the version of the app with the versions that are available. How can I compare the strings which are in the format "2.0.1"
and "2.0.09"
and get the highest one, in Objective-C?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如何使用
compare:options:
NSString 类的方法?How about using the
compare:options:
method of the NSString class?如果您的字符串都是“2.0.1”等形式,您可以将它们按原样与正确的选项进行比较:
如果 localVersion 不早于 currentVersion,上面将返回“
YES
”在服务器上,否则为“NO
”(假设我的方式正确)。这是检查 iDevice 上安装的 iOS 本地版本时通常要做的事情。
If your strings are all of the form "2.0.1" etc. you can just compare them as is with the right options:
The above would return "
YES
" if the localVersion is no older than the currentVersion on the server, and "NO
" otherwise (assuming I have this the right way round).This is the usual thing to do when checking the local version of iOS installed on an iDevice.
正如这篇文章中的回答; 比较 Objective-C 中的版本号
查看我实现简单版本的 NSString 类别检查github; https://github.com/stijnster/NSString-compareToVersion
这将返回一个 NSComparisonResult 比使用更准确;
还添加了助手;
As answered in this post; Compare version numbers in Objective-C
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 than using;
Helpers are also added;