Xcode warning "Property access results unused - getters should not be used for side effects"
Solution 1
The dot notation (i.e. self.fetchLetter
) is meant for properties, not for arbitrary methods. The self.fetchLetter
is being interpreted as "get the 'fetchLetter' property of 'self'," which isn't what you intend.
Just use [self fetchLetter]
instead.
Solution 2
In newer Xcode versions, even the [object method];
may trigger the warning. But sometimes we actually do need to call a property and discard the result, for example when dealing with view controllers and we need to make sure the view is actually loaded.
So we were doing:
// Ensure view is loaded and all outlets are connected.
[self view];
This now also triggers the “Property access results unused - getters should not be used for side effects” warning. The solution is to let the compiler know it's done intentionally by casting the result type to void:
(void)[self view];
Solution 3
You're declaring fetchLetter using syntax like this?
@property (retain) id fetchLetter;
That looks wrong for what you're doing. Properties are intended to be variable accessors that (in the case of getters) don't have any side effects.
You should declare fetchLetter as a method, like so:
- (void) fetchLetter;
and access it using:
[self fetchLetter]
ICL1901
OLD programmer - from the 60s (1960s at least), stumbling aimlessly through xCode, Obj-C, PHP, mySQL, sqLite, (x)html, css, js, bbc, COBOL, exoplanets, slashdot, Apple. #SOreadytohelp
Updated on September 30, 2020Comments
-
ICL1901 almost 4 years
I'm getting this warning when I'm calling a local routine.
My code is this:
-(void)nextLetter { // NSLog(@"%s", __FUNCTION__); currentLetter ++; if(currentLetter > (letters.count - 1)) { currentLetter = 0; } self.fetchLetter; }
I'm getting the warning on the self.fetchLetter statement.
That routine looks like this:
- (void)fetchLetter { // NSLog(@"%s", __FUNCTION__); NSString *wantedLetter = [[letters objectAtIndex: currentLetter] objectForKey: @"langLetter"]; NSString *wantedUpperCase = [[letters objectAtIndex: currentLetter] objectForKey: @"upperCase"]; ..... }
I prefer to fix warning messages, is there a better way to write this?
Thanks!
-
ICL1901 over 13 yearsHi Chris, I did declare fetchLetter as you suggest. I have now changed the declaration to [self fetchLetter] as you and Tom indicated.. I appreciate the understanding.
-
Dov over 9 yearsThis workaround no longer applies in Xcode 6.3 (beta)
-
Dov over 9 yearsI mean calling the selector instead of using dot notation. It technically shouldn't have probably made a difference, and now Clang still treats it the same way. @DarkDust's answer actually expresses the semantic intent of calling a selector just for its side effects
-
Tom Dalling over 9 years@Dov That is certainly true for property accessor methods (such as
[UIViewController view]
), but this question doesn't involve those. Still, I guess it's useful to know for the people who arrived here by googling the error message.