gpt4 book ai didi

ios - 使用不等式运算符比较对象

转载 作者:行者123 更新时间:2023-12-01 16:33:47 25 4
gpt4 key购买 nike

使用!=在我看来错了。我们正在比较对象,所以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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com