如何比较Objective-C中的对象?
是否像==一样简单?
我想检查一个对象的数组,如果它不存在,则将其添加到数组中,否则将其从数组中删除.
当我alloc和init两个NSString变量比较它们的指针时,它们是相同的.这是一个显示以下内容的片段:
NSString *s1 = [[NSString alloc] initWithString:@"hello world"];
NSString *s2 = [[NSString alloc] initWithString:@"hello world"];
if (s1 == s2) {
NSLog(@"==");
}else {
NSLog(@"!=");
}
Run Code Online (Sandbox Code Playgroud)
为什么s1和s2相同?