Xcode warning "Property access results unused - getters should not be used for side effects"

Objective CXcodeWarnings

Objective C Problem Overview


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!

Objective C Solutions


Solution 1 - Objective C

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 - Objective C

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 - Objective C

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]

Solution 4 - Objective C

I just got my problem resolved, in my case a CoreLocation Project, using both answers from Tom and Chris -

I declare:

@property (strong, nonatomic)CLLocationManager *locationManager;

And implemented like:

@synthesize locationManager = _locationManager;
....
- (void) dealloc {
         [self locationManager];
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionICL1901View Question on Stackoverflow
Solution 1 - Objective CTom DallingView Answer on Stackoverflow
Solution 2 - Objective CDarkDustView Answer on Stackoverflow
Solution 3 - Objective CChris DevereuxView Answer on Stackoverflow
Solution 4 - Objective CLAOMUSIC ARTSView Answer on Stackoverflow