tags:

views:

237

answers:

4

hi. 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_APP

anyone has an idea?

+1  A: 

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.

St3fan
Why the downvotes? this was a good answer.
kubi
"Try this instead" means "work around the real error and don’t learn from it". The problem of @Freaktor is clearly the shadowing of the instance variable. This wasn't explained in the first version af this answer. Also, the last thing about private instance variables is wrong.
Nikolai Ruhe
+1  A: 

If you really wanted to keep the path in an instance variable, just kill the first line.

  1. You don’t have to declare an instance variable in a method.
  2. You don’t have to initialize a variable with an empty string before assigning another string.
  3. You should then retain the instance variables object:

[_Ruta_APP autorelease];
_Ruta_APP = [[[NSBundle mainBundle] bundlePath] copy];

Nikolai Ruhe
this one did the trick
Freaktor
i means the another braket and the copy.
Freaktor
Actually, there are quite a few of the details of this answer that "did the trick". I would suggest that you strive to understand the details of the differences of the code in the answer and your original code or else you risk repeating the same errors over and over.
bbum
sorry for the late, i wasnt aware of this _Ruta_APP = [[NSBundle mainBundle] bundlePath]; with this i was copying the pointer of the nsbundle not the content. and with "copy" the content is copied into a new NSString
Freaktor
A: 
  1. 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.

  2. 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.

Graham Lee
thanks. but if i remove it the warning does not go away. it seems that i got an new instance of the object nsbundle and the complilator just goes insane :P
Freaktor
A: 

(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.

Peter Hosey
i got it. i just started with objective c some weeks ago. and english is not my home language, thanks for your advice and i will get to debug all my code.
Freaktor