使用!=在我看来错了。我们正在比较对象,所以isEqual应该是正确的方法吗?
除非NSNull null返回具有一致内存地址的单例?
if ([super actionForLayer:layer forKey:@"backgroundColor"] != [NSNull null]) {
// whatever
}
尼克·洛克伍德(Nick Lockwood)在要点here中使用了它
最佳答案
==运算符检查两个指针是否指向相同的对象,而isEqual
检查对象的内容是否相同,但是两个对象指针可能未指向相同的地址。
NSString *s1 = [NSString stringWithFormat:@"Hello"];
NSString *s2 = s1;
NSString *s3 = [NSString stringWithFormat:@"Hello"];
if (s1 == s2)
NSLog(@"result 1");
if (s2 == s3)
NSLog(@"result 2");
if ([s1 isEqual:s2])
NSLog(@"result 3");
if ([s2 isEqual:s3])
NSLog(@"result 4");
if (!([s2 isEqual:s3])) //Not equal for objects (change value to see)
NSLog(@"result 5");
//prints pointer addresses
NSLog(@"%p", s1);
NSLog(@"%p", s2);
NSLog(@"%p", s3);
关于ios - 使用不等式运算符比较对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30116463/