LooN3y Posted April 19, 2012 Share Posted April 19, 2012 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 https://www.insanelymac.com/forum/topic/278298-how-to-get-rid-of-unused-variable-warning/ Share on other sites More sharing options...
TH3L4UGH1NGM4N Posted April 20, 2012 Share Posted April 20, 2012 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 https://www.insanelymac.com/forum/topic/278298-how-to-get-rid-of-unused-variable-warning/#findComment-1814983 Share on other sites More sharing options...
Poco Posted April 20, 2012 Share Posted April 20, 2012 [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 https://www.insanelymac.com/forum/topic/278298-how-to-get-rid-of-unused-variable-warning/#findComment-1815035 Share on other sites More sharing options...
TH3L4UGH1NGM4N Posted April 21, 2012 Share Posted April 21, 2012 @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. Link to comment https://www.insanelymac.com/forum/topic/278298-how-to-get-rid-of-unused-variable-warning/#findComment-1815067 Share on other sites More sharing options...
Poco Posted April 21, 2012 Share Posted April 21, 2012 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 https://www.insanelymac.com/forum/topic/278298-how-to-get-rid-of-unused-variable-warning/#findComment-1815072 Share on other sites More sharing options...
TH3L4UGH1NGM4N Posted April 21, 2012 Share Posted April 21, 2012 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 https://www.insanelymac.com/forum/topic/278298-how-to-get-rid-of-unused-variable-warning/#findComment-1815078 Share on other sites More sharing options...
LooN3y Posted April 24, 2012 Author Share Posted April 24, 2012 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 https://www.insanelymac.com/forum/topic/278298-how-to-get-rid-of-unused-variable-warning/#findComment-1815868 Share on other sites More sharing options...
Poco Posted April 24, 2012 Share Posted April 24, 2012 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 https://www.insanelymac.com/forum/topic/278298-how-to-get-rid-of-unused-variable-warning/#findComment-1815882 Share on other sites More sharing options...
LooN3y Posted April 24, 2012 Author Share Posted April 24, 2012 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 https://www.insanelymac.com/forum/topic/278298-how-to-get-rid-of-unused-variable-warning/#findComment-1815945 Share on other sites More sharing options...
Poco Posted April 25, 2012 Share Posted April 25, 2012 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 https://www.insanelymac.com/forum/topic/278298-how-to-get-rid-of-unused-variable-warning/#findComment-1815950 Share on other sites More sharing options...
TH3L4UGH1NGM4N Posted May 4, 2012 Share Posted May 4, 2012 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 https://www.insanelymac.com/forum/topic/278298-how-to-get-rid-of-unused-variable-warning/#findComment-1818118 Share on other sites More sharing options...
Recommended Posts