简体   繁体   English

不使用属性访问结果,但NSLog可以正常工作

[英]Property access results unused but NSLog works fine

I'm trying to display results in a text label, but I get different result than NSLog: 我试图在文本标签中显示结果,但是得到的结果与NSLog不同:

if( [elementName isEqualToString:@"CommunityID"])
    {
        self.recordResults = FALSE;
        ResultLabel.text = @"CommunityID: %@", self.soapResults;
        NSLog(@"CommunityID:%@",self.soapResults);
        self.soapResults = nil;
    }

NSlog correctly shows the text result, but the UILabel doesn't. NSlog可以正确显示文本结果,但UILabel不能。 The error shows: 错误显示:

"Property access result unused - getters should not be used for side effects" “未使用属性访问结果-吸气剂不应用于副作用”

I don't understand how NSlog gets the info just fine but the other doesn't? 我不明白NSlog如何很好地获取信息,而其他人却不知道? Any ideas? 有任何想法吗?

您不能直接分配,需要使用stringWithFormat属性

ResultLabel.text = [NSString stringWithFormat:@"CommunityID: %@", self.soapResults];

Just so you know what was happening and what the compiler warning was about… 如此您就知道发生了什么以及编译器警告是关于什么的……

In C and Objective-C, a comma ( , ) that's not separating arguments in a function or method call is the comma operator. 在C和Objective-C中,不分隔函数或方法调用中的参数的逗号( , )是逗号运算符。 It is a compound expression. 它是一个复合表达式。 The left-hand sub-expression is evaluated but its result is discarded. 评估左手子表达式,但其结果被丢弃。 Then, the right-hand sub-expression is evaluated and the overall compound expression takes its value. 然后,评估右手子表达式,并且整个复合表达式取其值。

In your case, the compound expression was just used as a statement and its result was not used. 在您的情况下,复合表达式仅用作语句,而未使用其结果。 So, your statement: 因此,您的声明:

ResultLabel.text = @"CommunityID: %@", self.soapResults;

was the equivalent of: 等于:

ResultLabel.text = @"CommunityID: %@";
self.soapResults;

The second of those statements calls a property getter and discards the resulting value. 这些语句的第二个调用属性获取器,并丢弃结果值。 The compiler is warning you. 编译器警告您。 Either you didn't mean to do that (as in this case) or you were invoking the getter because the getter has side effects that you wanted, which is a really bad idea. 要么您不是要这样做(在这种情况下),要么是您要调用吸气剂,因为吸气剂具有您想要的副作用,这是一个非常糟糕的主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM