k3a*_*k3a 3 cocoa-touch objective-c string-comparison nsstring
为什么这种比较会导致NO?
BOOL areTheSame = NSOrderedSame == [@"th" compare:@"They" options:NSCaseInsensitiveSearch range:NSMakeRange(0, 2)];
Run Code Online (Sandbox Code Playgroud)
当我测试它时@"th",@"Th"它就是YES.
我在这里错过了什么?
这是违反直觉的,但该range论点仅适用于接收者.另一个字符串的长度(参数compare:)不受范围限制.您的调用会缩小@"th"到范围{0,2},这会产生@"th"(即,这没有效果),然后将其与之比较@"They".
你会看到这个:
NSComparisonResult comp = [@"They" compare:@"th"
options:NSCaseInsensitiveSearch
range:NSMakeRange(0, 2)];
BOOL areTheSame = comp == NSOrderedSame;
Run Code Online (Sandbox Code Playgroud)
产生你期望的结果,因为它将接收器(@"They")向下(向下@"Th"),然后进行比较.
| 归档时间: |
|
| 查看次数: |
1013 次 |
| 最近记录: |