我可以简单地使用
if(myString == nil)
由于某种原因,我知道的字符串为null,这个语句失败了.
您的字符串可能实际上nil
不是,而只是一个空字符串?您可以尝试测试是否[myString length] == 0
.
您可以在此处找到有关目标C字符串的更多信
+ (BOOL ) stringIsEmpty:(NSString *) aString { if ((NSNull *) aString == [NSNull null]) { return YES; } if (aString == nil) { return YES; } else if ([aString length] == 0) { return YES; } else { aString = [aString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; if ([aString length] == 0) { return YES; } } return NO; } + (BOOL ) stringIsEmpty:(NSString *) aString shouldCleanWhiteSpace:(BOOL)cleanWhileSpace { if ((NSNull *) aString == [NSNull null]) { return YES; } if (aString == nil) { return YES; } else if ([aString length] == 0) { return YES; } if (cleanWhileSpace) { aString = [aString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; if ([aString length] == 0) { return YES; } } return NO; }
你可以用这个检查你的字符串
if(myString==(id) [NSNull null] || [myString length]==0 || [myString isEqualToString:@""]) { //String is null or bad response }