warning on nsbundle bundlepath
i want to get the executable path of my bundle. (i want to get the path so i can load images in a NSImageView)
i got this.
NSString * _Ruta_APP = [[NSString alloc] init];
_Ruta_APP = [[NSBundle mainBundle] bundlePath];
but the compiler says /ControlAPP.m:33:0 /ControlAPP.m:33: warning: local declaration of '_Ruta_APP' hides instance variable
but i cannot use the value of _Ruta_AP开发者_JS百科P
anyone has an idea?
If you really wanted to keep the path in an instance variable, just kill the first line.
- You don’t have to declare an instance variable in a method.
- You don’t have to initialize a variable with an empty string before assigning another string.
- You should then retain the instance variables object:
[_Ruta_APP autorelease];
_Ruta_APP = [[[NSBundle mainBundle] bundlePath] copy];
Couple of things:
Try this instead:
NSString* imagePath = [[NSBundle mainBundle] pathForResource @"SomeImage" ofType: @"png"]
The warning that you are getting seems to indicate that you also have an instance variable with the same name as that local variable in your code snippet.
Instance variables with underscores are probably also a bad idea since that is what Apple uses for hidden/private ivars. I think it is considered bad style to use them in your own code.
It seems like you have a variable called _Ruta_APP, as well as an instance variable of the same name. If you mean to use the instance variable, you don't need to redefine the variable inside the method.
The first line in your snippit creates an object that you never use, and which leaks.
So I'd say just remove the first line from your snippit and the warning should go away.
(i want to get the path so i can load images in a NSImageView)
You don't need the path to your executable to do that. The easiest way is NSImage's imageNamed:
method; the second easiest is what St3fan suggested.
Now let's go through the problems in your implementation of the hard way:
NSString * _Ruta_APP = [[NSString alloc] init];
This declares a local variable named _Ruta_APP
and initializes it to hold an NSString object, which you own because you created it with alloc
and have not released it.
_Ruta_APP = [[NSBundle mainBundle] bundlePath];
This puts a different string object into the same variable, replacing the first one. If you're not using garbage collection, then that first object is still alive and you still own it, even though you no longer have a way to send it messages. Thus, you have leaked it.
If you meant to have _Ruta_APP
as an instance variable, then cut the entire first line. It's generally a bad idea to hold objects that you don't own in your instance variables, so take ownership of this object; the best way would be to make a copy (after doing so, you will own the copy) and put that in the instance variable. Otherwise, when whatever does own the original object releases it, the object will die, but you'll still be holding it; then you'll send a message to a dead object, which will crash your app. See the memory management rules.
If you meant to have _Ruta_APP
as a local variable, not in any other instance methods, cut the instance variable.
精彩评论