使用“compare”比较NSString,就像Java的compareTo
我正在尝试从数据库中提取我的应用程序版本(1.2.0),并将其与已安装的应用程序的当前版本进行比较。它可能是未定义的或更低的(例如 1.1.0 等..)。
我知道我可以在 NSString 上使用 isEqualTo 但这只是比较相等性。
我想知道如何使用compare来获得类似于Java的compareTo方法的结果,该方法返回整数负数、0、正数。
我查过 NSComparisonResult 但不知道如何使用访问结果。
另外,顺便说一句:是否可以假设更新之前的版本低于苹果应用程序中的更新版本?应用程序是否可能被降级,以便当应用程序运行时数据库可能属于较新版本的应用程序?
谢谢。
I'm trying to pull my app version from my DB which is 1.2.0 and compare it to the installed apps current version. which may be undefined or lower (e.g. 1.1.0 etc..).
I know I can use isEqualTo on NSString but that just compare's for equality.
I'd like to know how to use compare for a result similar to Java's compareTo method which return an integer negative, 0, positive.
I've looked up NSComparisonResult but can't figure out how to use access the results.
Also, on a side note: Is it ok to assume the version previous to the update is lower that update version in apple apps? Is it possible that an app be downgraded so when an app runs the database might belong to a newer version of app?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 NSString:: Compare 比较两个 NSString。
例如:
请注意,
retValue
的结果是两个字符串是否相等。枚举NSComparisonResult
的定义 -如果
retValue == NSOrderedSame
,则两个字符串相等。即返回值为0。You can use NSString::compare to compare two NSStrings.
Ex:
Notice that the
retValue
has the result of whether the two strings are equal or not. The defintion of the enumNSComparisonResult
-If
retValue == NSOrderedSame
, then the two strings are equal. i.e., return value is 0.如何使用 比较?生成的 NSComparisonResult 只是 -1、0、1 值的枚举。
How about using compare? The resulting NSComparisonResult is just an enum of the -1, 0, 1 values.
1)NS比较结果
NSOrderedAscending = -1, NSOrderedSame, NSOrderedDescending
这意味着如果第一个值小于第二个值,则为 -1,如果值等于 0,则为 1。
2) AppStore 无法降级。如果版本不匹配,则可以假设 AppStore 版本较新。
1) NSComparisonResult
NSOrderedAscending = -1, NSOrderedSame, NSOrderedDescending
That means if first value is smaller than second, you will get -1, if values are equal 0, otherwise 1.
2) Downgrade not possible for AppStore. If versions do not match, it's ok to assume the AppStore one is newer.