将字符串与格式“2.0.1”、“2.0.09”进行比较

发布于 2024-11-29 01:17:49 字数 191 浏览 1 评论 0原文

我需要获取用户的应用程序版本号并将其与服务器上当前的应用程序版本进行比较。如果用户的应用程序版本较低,那么他将收到一个弹出窗口来更新他的应用程序。在执行此操作时,我需要将应用程序的版本与可用版本进行比较。在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白况 2024-12-06 01:17:49

如何使用 compare:options: NSString 类的方法?

NSString *v1 = @"2.0.1";
NSString *v2 = @"2.1";

NSComparisonResult result = [v1 compare:v2 options:NSNumericSearch];
if (result == NSOrderedSame || result == NSOrderedDescending) {
    // do
} else {
    // do
}

How about using the compare:options: method of the NSString class?

NSString *v1 = @"2.0.1";
NSString *v2 = @"2.1";

NSComparisonResult result = [v1 compare:v2 options:NSNumericSearch];
if (result == NSOrderedSame || result == NSOrderedDescending) {
    // do
} else {
    // do
}
惯饮孤独 2024-12-06 01:17:49

如果您的字符串都是“2.0.1”等形式,您可以将它们按原样与正确的选项进行比较:

([localVersionString compare:currentVersionString
                     options:NSNumericSearch] != NSOrderedAscending);

如果 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:

([localVersionString compare:currentVersionString
                     options:NSNumericSearch] != NSOrderedAscending);

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.

心凉 2024-12-06 01:17:49

正如这篇文章中的回答; 比较 Objective-C 中的版本号

查看我实现简单版本的 NSString 类别检查github; https://github.com/stijnster/NSString-compareToVersion

[@"1.2.2.4" compareToVersion:@"1.2.2.5"];

这将返回一个 NSComparisonResult 比使用更准确;

[@"1.2.2" compare:@"1.2.2.5" options:NSNumericSearch]

还添加了助手;

[@"1.2.2.4" isOlderThanVersion:@"1.2.2.5"];
[@"1.2.2.4" isNewerThanVersion:@"1.2.2.5"];
[@"1.2.2.4" isEqualToVersion:@"1.2.2.5"];
[@"1.2.2.4" isEqualOrOlderThanVersion:@"1.2.2.5"];
[@"1.2.2.4" isEqualOrNewerThanVersion:@"1.2.2.5"];

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

[@"1.2.2.4" compareToVersion:@"1.2.2.5"];

This will return a NSComparisonResult which is more accurate than using;

[@"1.2.2" compare:@"1.2.2.5" options:NSNumericSearch]

Helpers are also added;

[@"1.2.2.4" isOlderThanVersion:@"1.2.2.5"];
[@"1.2.2.4" isNewerThanVersion:@"1.2.2.5"];
[@"1.2.2.4" isEqualToVersion:@"1.2.2.5"];
[@"1.2.2.4" isEqualOrOlderThanVersion:@"1.2.2.5"];
[@"1.2.2.4" isEqualOrNewerThanVersion:@"1.2.2.5"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文