【发布时间】:2014-07-29 16:29:06
【问题描述】:
我有以下代码
NSString *firstName = @"1.6.1";
NSString *secondName = @"1.6.1";
if (!(firstName==secondName)) {
NSLog(@"lock the app");
} else {
NSLog(@"do not lock the app");
}
if (!([firstName isEqualToString:secondName])) {
NSLog(@"lock the app");
} else {
NSLog(@"do not lock the app");
}
我得到的输出是
do not lock the app
do not lock the app
但是,当我使用 firstName 和 secondName 的实际值时,我得到的输出为
lock the app
do not lock the app
以下是firstName和secondName的详细信息
// this is coming from server
firstName = [[NSUserDefaults standardUserDefaults] stringForKey:@"iPhoneAppVersion"];
// this is coming from app version from iPhone
secondName = [self appNameAndVersionNumberDisplayString];
- (NSString *)appNameAndVersionNumberDisplayString {
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString *appDisplayName = [infoDictionary objectForKey:@"CFBundleDisplayName"];
NSString *majorVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
NSString *minorVersion = [infoDictionary objectForKey:@"CFBundleVersion"];
return [NSString stringWithFormat:@"%@",
minorVersion];
}
如果我打印firstName 和secondName,我得到的值分别为1.6.1、1.6.1。
知道为什么在使用 equals 时会有两个不同的输出吗?
【问题讨论】:
-
在您从服务器收到的内容的开头/结尾处可能有一个(隐藏的)空格字符。也尝试记录字符串的长度。
-
@Cyrille :我检查过,长度相同,没有空格字符...
标签: objective-c nsstring equals