nsbundle 捆绑路径上的警告

发布于 2024-08-20 07:21:53 字数 320 浏览 10 评论 0原文

我想获取我的包的可执行路径。 (我想获取路径,以便我可以在 NSImageView 中加载图像)

我得到了这个。

NSString * _Ruta_APP = [[NSString alloc] init];
_Ruta_APP = [[NSBundle mainBundle] bundlePath];

但编译器说 /ControlAPP.m:33:0 /ControlAPP.m:33:警告:“_Ruta_APP”的本地声明隐藏了实例变量

,但我无法使用 _Ruta_APP 的值,

有人知道吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

远山浅 2024-08-27 07:21:53

如果您确实想将路径保留在实例变量中,只需删除第一行即可。

  1. 您不必在方法中声明实例变量。
  2. 在分配另一个字符串之前,不必使用空字符串初始化变量。
  3. 然后,您应该保留实例变量对象:


[_Ruta_APP 自动释放];
_Ruta_APP = [[[NSBundle mainBundle] bundlePath] 复制];

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];

梓梦 2024-08-27 07:21:53

有几件事:

请尝试这样做:

NSString* imagePath = [[NSBundle mainBundle] pathForResource @"SomeImage" ofType: @"png"]

您收到的警告似乎表明您还有一个与代码片段中的局部变量同名的实例变量。

带下划线的实例变量可能也是一个坏主意,因为这是 Apple 用于隐藏/私有 ivars 的方式。我认为在您自己的代码中使用它们被认为是不好的风格。

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.

梅倚清风 2024-08-27 07:21:53
  1. 您似乎有一个名为 _Ruta_APP 的变量,以及一个同名的实例变量。如果您打算使用实例变量,则不需要在方法内重新定义变量。

  2. 代码片段中的第一行创建了一个您从未使用过且发生泄漏的对象。

    代码

所以我想说,只需从片段中删除第一行,警告就会消失。

  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.

荆棘i 2024-08-27 07:21:53

(我想获取路径,以便我可以在 NSImageView 中加载图像)

您不需要可执行文件的路径来执行此操作。最简单的方法是 NSImage 的 imageNamed: 方法;第二个最简单的是 St3fan 的建议。

现在让我们通过困难的方式解决一下实现中的问题:

NSString * _Ruta_APP = [[NSString alloc] init];

这声明了一个名为 _Ruta_APP 的局部变量,并将其初始化以保存一个 NSString 对象,您拥有该对象,因为您使用 alloc< 创建了它/code> 并没有发布它。

_Ruta_APP = [[NSBundle mainBundle] bundlePath];

这会将不同的字符串对象放入同一变量中,替换第一个字符串对象。如果您不使用垃圾回收,那么第一个对象仍然存在,并且您仍然拥有它,即使您不再有办法向它发送消息。因此,你已经泄露了它。

如果您打算将 _Ruta_APP 作为实例变量,则剪切整个第一行。在实例变量中保存不属于您的对象通常是一个坏主意,因此请获得该对象的所有权;最好的方法是制作一个副本(这样做后,您将拥有该副本)并将其放入实例变量中。否则,当拥有原始对象的任何东西释放它时,该对象就会死亡,但你仍然会持有它;那么你将向一个死亡对象发送一条消息,这将使你的应用程序崩溃。请参阅内存管理规则

如果您打算将 _Ruta_APP 作为局部变量,而不是在任何其他实例方法中,请剪切实例变量。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文