iOS compare用法
生活随笔
收集整理的這篇文章主要介紹了
iOS compare用法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今天在看一個bug,原來是之前同事寫的代碼用compare比較兩個字符串,(我對這個compare也是理解有誤),看了文檔,原來是這樣的:
/* In the compare: methods, the range argument specifies the subrange, rather than the whole, of the receiver to use in the comparison. The range is not applied to the search string.? For example, [@"AB" compare:@"ABC" options:0 range:NSMakeRange(0,1)] compares "A" to "ABC", not "A" to "A", and will return NSOrderedAscending.
*/
- (NSComparisonResult)compare:(NSString *)string;
如果沒有制定比較的范圍,那么這個compare只會默認比較第一個字符,第一個字符大于第二個字符,也就是說即便是[”8“ compare ”10“ ]也會輸出降序(心里想的是升序);
反之,第一個字符小于第二個字符,那么就輸出升序。看下圖:
總結
以上是生活随笔為你收集整理的iOS compare用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦醒
- 下一篇: 好物推荐:文字转语音朗读软件哪个好?