Jump to content
Welcome to InsanelyMac Forum

Register now to gain access to all of our features. Once registered and logged in, you will be able to contribute to this site by submitting your own content or replying to existing content. You'll be able to customize your profile, receive reputation points as a reward for submitting content, while also communicating with other members via your own private inbox, plus much more! This message will be removed once you have signed in.

  • Announcements

    • Allan

      Forum Rules   04/13/2018

      Hello folks! As some things are being fixed, we'll keep you updated. Per hour the Forum Rules don't have a dedicated "Tab", so here is the place that we have our Rules back. New Users Lounge > [READ] - InsanelyMac Forum Rules - The InsanelyMac Staff Team. 
Sign in to follow this  
LooN3y

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?

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

@iPoco I'm assuming he created a timer with the intent of using it on the GPS at 5 second intervals judging from the snippet of code. Doubt he'd want to delete it if he needs it.

Share this post


Link to post
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];

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×