Jump to content

How To Get Rid Of Unused Variable Warning?


  • Please log in to reply
10 replies to this topic

#1
LooN3y

LooN3y

    InsanelyMac Protégé

  • Members
  • Pip
  • 27 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?

#2
TH3L4UGH1NGM4N

TH3L4UGH1NGM4N

    (~_~)

  • News Team
  • 1,156 posts
  • Gender:Male
  • Location:Wonderland
  • Interests:(~_^)
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.

#3
Poco

Poco

    InsanelyMac Plumber

  • Supervisors
  • 1,478 posts
  • Gender:Male
  • Location:Canada
[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.ap...000994-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.

#4
TH3L4UGH1NGM4N

TH3L4UGH1NGM4N

    (~_~)

  • News Team
  • 1,156 posts
  • Gender:Male
  • Location:Wonderland
  • Interests:(~_^)
@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.

#5
Poco

Poco

    InsanelyMac Plumber

  • Supervisors
  • 1,478 posts
  • Gender:Male
  • Location:Canada
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];



#6
TH3L4UGH1NGM4N

TH3L4UGH1NGM4N

    (~_~)

  • News Team
  • 1,156 posts
  • Gender:Male
  • Location:Wonderland
  • Interests:(~_^)
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.

#7
LooN3y

LooN3y

    InsanelyMac Protégé

  • Members
  • Pip
  • 27 posts

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.

#8
Poco

Poco

    InsanelyMac Plumber

  • Supervisors
  • 1,478 posts
  • Gender:Male
  • Location:Canada
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.

#9
LooN3y

LooN3y

    InsanelyMac Protégé

  • Members
  • Pip
  • 27 posts

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

#10
Poco

Poco

    InsanelyMac Plumber

  • Supervisors
  • 1,478 posts
  • Gender:Male
  • Location:Canada
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.

#11
TH3L4UGH1NGM4N

TH3L4UGH1NGM4N

    (~_~)

  • News Team
  • 1,156 posts
  • Gender:Male
  • Location:Wonderland
  • Interests:(~_^)
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.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

© 2014 InsanelyMac  |   News  |   Forum  |   Downloads  |   OSx86 Wiki  |   Mac Netbook  |   PHP hosting by CatN  |   Designed by Ed Gain  |   Logo by irfan  |   Privacy Policy