Jump to content

How To Get Rid Of Unused Variable Warning?


11 posts in this topic

Recommended Posts

basically i have this warning, its nothing big, it just bothers me that its there.

 

heres my code:

 

 

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   GlobalStrings* theDataObject = [self theGlobalClass];
   theDataObject.deviceUDID =  [[uIDevice currentDevice] name];

   self.locationManager = [[CLLocationManager alloc] init];
   locationManager.desiredAccuracy = kCLLocationAccuracyBest;
   locationManager.delegate = self;
   [locationManager startUpdatingLocation];

   NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(sendGPS) userInfo:nil repeats:YES];

 

 

 

 

 

 

i used to have "timer release" at the bottom, but it had errors as this method is connected with another method in the app delegate.

 

 

so i had to get rid of it from crashing.

 

 

is there anything i can just put in here that useless that'll get rid of it?

Link to comment
Share on other sites

It might be because you're initializing the timer and not using it which is why Xcode complains about the unused var since it takes up memory in the app.

 

You could of course turn off those warnings manually but I'd advise you don't since the vars do take up little bits of memory and you'd want to keep your app running as efficiently as possible.

Link to comment
Share on other sites

[NSTimer scheduled...] like [NSString stringWithString:aString] returns an autoreleased object, so you are not responsible for releasing the timer. You are conventionally only responsible for objects you create with alloc, copy, mutableCopy, new, etc.

I recommend you read this:

https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/memorymgmt/Articles/mmRules.html#//apple_ref/doc/uid/20000994-BAJHFBGH

 

It depends what you want to do with the timer. If you need access to it in another method, then you can store it as a ivar. If not then just remove the "NSTimer *timer" line from your code. You will be ignoring the returned object which you don't need.

Link to comment
Share on other sites

You can still use the timer by ignoring the returned object. He could change his code from:

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(sendGPS) userInfo:nil repeats:YES];

To

[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(sendGPS) userInfo:nil repeats:YES];

 

Unless he is interacting with the timer later during that method (which he isn't based on the warning), there is no need to define a variable.

If you need access to the timer from another method (for instance for invalidating the timer), then you can store it as an ivar.

 

Out of a separate point, the method must take the timer as a parameter. So the method should really be named

- (void)sendGPS:(NSTimer *)aTimer

and the line setting up the timer

[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(sendGPS:) userInfo:nil repeats:YES];

Link to comment
Share on other sites

Ahh I see what you're getting at now makes sense to me now. He might not need the timer anywhere else though since he's using it in the

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

method.

Link to comment
Share on other sites

You can still use the timer by ignoring the returned object. He could change his code from:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(sendGPS) userInfo:nil repeats:YES];

To

[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(sendGPS) userInfo:nil repeats:YES];

Unless he is interacting with the timer later during that method (which he isn't based on the warning), there is no need to define a variable.

If you need access to the timer from another method (for instance for invalidating the timer), then you can store it as an ivar.

Out of a separate point, the method must take the timer as a parameter. So the method should really be named

- (void)sendGPS:(NSTimer *)aTimer

and the line setting up the timer

[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(sendGPS:) userInfo:nil repeats:YES];

 

 

 

so i should change it to this? since that code calls the send gps and not the other way around it should be fine?

 

 

 

i put the methods send gps and void location manager below.

Link to comment
Share on other sites

I'm not entirely sure what you mean?

 

The method called by the timer must have a signature like below as says the NSTimer documentation.

- (void)timerFireMethod:(NSTimer*)theTimer

 

The other change to the NSTimer line just adds a ":" in the selector parameter to call a method named "sendGPS" taking 1 parameter.

Link to comment
Share on other sites

I'm not entirely sure what you mean?

 

The method called by the timer must have a signature like below as says the NSTimer documentation.

- (void)timerFireMethod:(NSTimer*)theTimer

 

The other change to the NSTimer line just adds a ":" in the selector parameter to call a method named "sendGPS" taking 1 parameter.

 

 

im apologize, as you can see I'm a noob, I've only started learning programming for the very first time in september.

 

 

basically I'm asking if i change my code to yours (which looks like the much better way to write it) would it mess anything up? considering it'll get rid of the warning that i do not want, just as a personal preference

Link to comment
Share on other sites

It should make things work better! The timer likely wouldn't fire correctly without changing the method name, although I've never tried so I can't say for sure. It also gives you the option of something like below without keeping tabs on the timer yourself.

- (void)sendGPS:(NSTimer *)aTimer
{
    if (aCondition) //Some condition that you want to check
    {
         [aTimer invalidate]; //Stops the timer
    }
}

 

The other change above by removing the "NSTimer *timer" is just to get rid of the warning and doesn't have any effect on your code. When you allocate an NSTimer object using [NSTimer scheduled....] it will setup, start the timer using your parameters and return it. By having the "NSTimer *timer" part, you are giving yourself a way to access the timer that you just set up. In most cases a scheduled timer doesn't need any interaction, so it's safe to ignore.

Link to comment
Share on other sites

  • 2 weeks later...

Also keep in mind Xcode throws the warning because when you just needlessly allocate memory and not use it it's essentially being wasted. A timer may not take up that much space but look on it on a more grand scale if you were to allocate an array of 3000 objects and not use it you'll probably see a memory difference and/or leak.

 

Just try to keep your projects not really warning free because that's difficult at times but leave unused ivars lying around they occupy space ya know.

Link to comment
Share on other sites

 Share

×
×
  • Create New...