比较Objective-C中的版本号 [英] Compare version numbers in Objective-C

查看:316
本文介绍了比较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"?

推荐答案

比较版本的方式,记住1 1.0 1.0.0:

This is the simplest way to compare versions, keeping in mind that "1" < "1.0" < "1.0.0":

NSString* requiredVersion = @"1.2.0";
NSString* actualVersion = @"1.1.5";

if ([requiredVersion compare:actualVersion options:NSNumericSearch] == NSOrderedDescending) {
  // actualVersion is lower than the requiredVersion
}

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

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