天天看點

object_c字元串比較之:版本号

在開發當中,經常會比較軟體的版本号,比如1.0.0.1與1.0.1。方法有很多,你可以一個一個解析出來,比較數值。其實有一種NSString的内置比較方法,隻不過帶了比較選擇項,多一個參數而已。下面的源碼擴充了系統的NSString功能,術語叫:category。

@implementation NSString(Version)

-(BOOL) isOlderVersionThan:(NSString*)otherVersion

{

return ([self compare:otherVersion options:NSNumericSearch] == NSOrderedAscending);

}

-(BOOL) isNewerVersionThan:(NSString*)otherVersion

{

return ([self compare:otherVersion options:NSNumericSearch] == NSOrderedDescending);

}

@end 

工程源碼