简体   繁体   中英

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:

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. 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? 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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