Xcode Warning "Property Access Results Are Not Used - Getters Should Not Be Used For Side Effects"

I get this warning when I call a local procedure.

My code is:

-(void)nextLetter { // NSLog(@"%s", __FUNCTION__); currentLetter ++; if(currentLetter > (letters.count - 1)) { currentLetter = 0; } self.fetchLetter; } 

I get a warning in the self.fetchLetter statement.

This procedure is as follows:

 - (void)fetchLetter { // NSLog(@"%s", __FUNCTION__); NSString *wantedLetter = [[letters objectAtIndex: currentLetter] objectForKey: @"langLetter"]; NSString *wantedUpperCase = [[letters objectAtIndex: currentLetter] objectForKey: @"upperCase"]; ..... } 

I prefer to correct warning messages, is there a better way to write this?

Thank!

+45
objective-c xcode warnings
Mar 17 '11 at 23:55
source share
4 answers

Dot notation (i.e. self.fetchLetter ) is for properties, not arbitrary methods. self.fetchLetter interpreted as "get the property" fetchLetter "for" I "," this is not what you intend to. "

Just use [self fetchLetter] instead.

+104
Mar 18 2018-11-11T00:
source share
— -

In newer versions of Xcode, even [object method]; may cause a warning. But sometimes we really need to call a property and discard the result, for example, when working with view controllers, and we need to make sure that the view is really loaded.

So we did:

 // Ensure view is loaded and all outlets are connected. [self view]; 

This also leads to the warning "Invalid result of accessing resources is not used, and not to prevent side effects." The solution is to let the compiler know that this was done intentionally, by casting the result type in void:

 (void)[self view]; 
+11
Feb 20 '15 at 11:19
source share

Do you declare fetchLetter using syntax like this?

 @property (retain) id fetchLetter; 

This does not look right for what you are doing. Properties are intended to be used as variable accessories, which (in the case of getters) do not have side effects.

You must declare fetchLetter as a method, for example:

 - (void) fetchLetter; 

and access it using:

 [self fetchLetter] 
+4
Mar 18 2018-11-11T00:
source share

I just solved the problem, in my case the CoreLocation project using both answers from Tom and Chris -

I declare:

 @property (strong, nonatomic)CLLocationManager *locationManager; 

And implemented as:

 @synthesize locationManager = _locationManager; .... - (void) dealloc { [self locationManager]; } 
0
02 Oct '12 at 8:15
source share



All Articles