Objective c 量程不小于´;不能识别子串

Objective c 量程不小于´;不能识别子串,objective-c,Objective C,我有这种行为,我无法理解 NSString *title = [[PushNotifcationManager getPushNotificationSettingsForKey:@"settings"] objectForKey:@"title"]; NSLog(@"title: %@",title); //Outputs Test\ntest if([title rangeOfString:@"\n"].location == NSNotFound){ //String contain

我有这种行为,我无法理解

NSString *title = [[PushNotifcationManager getPushNotificationSettingsForKey:@"settings"] objectForKey:@"title"];
NSLog(@"title: %@",title); //Outputs Test\ntest
if([title rangeOfString:@"\n"].location == NSNotFound){
   //String contains no \n
}else{
   //String contains \n
}
即使字符串明确包含“\n”
[title rangeOfString:@“\n”].location==NSNotFound
返回
true
NSLog(%@”)
不会转义字符串中的反斜杠字符,因此输出
Test\ntest
表示字符串包含反斜杠字符,后跟
n
, 不是换行符

因此,您必须检查

[title rangeOfString:@"\\n"]
其中,字符串文字中的
\\
表示一个反斜杠字符