applicationDidFinishLaunching 变量范围问题?

发布于 2024-12-26 03:55:24 字数 402 浏览 5 评论 0原文

只是一个简单的问题。我有一个在我的 applicationDidFinishLaunching 方法中生成的 NSArray,但由于某种原因该数组没有被其他方法使用,并且它说它未被使用。

-(void)applicationDidFinishLaunching... {
    NSArray* songsArray = [root nodesForXPath:@".//dict/dict/dict" error:nil];

-(id)tableView:(NSTableView *)tableView objectValueForTableColumn... {
    for(NSXMLElement* song in songsArray) {

我也在头文件中声明了该变量。

干杯, 斯科特

Just a quick question. I have an NSArray that is generated in my applicationDidFinishLaunching method, but for some reason the array isn't being used by other methods and it is saying that it is being unused.

-(void)applicationDidFinishLaunching... {
    NSArray* songsArray = [root nodesForXPath:@".//dict/dict/dict" error:nil];

-(id)tableView:(NSTableView *)tableView objectValueForTableColumn... {
    for(NSXMLElement* song in songsArray) {

I have declared the variable in the header file also.

Cheers,
Scott

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

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

发布评论

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

评论(3

薔薇婲 2025-01-02 03:55:24

您可能已在标头中声明了 songsArray,但未在 -application:didFinishLaunching: 中分配它。相反,您创建了一个具有相同名称的局部变量,该变量只会保留在范围内直到方法结束。像这样分配你的 ivar:

songsArray = [root nodesForXPath:@".//dict/dict/dict" error:nil];

You may have declared a songsArray in the header, but you're not assigning it in -application:didFinishLaunching:. Instead you have created a local variable with the same name that is only going to stay in scope until the end of the method. Assign your ivar like this:

songsArray = [root nodesForXPath:@".//dict/dict/dict" error:nil];
世界如花海般美丽 2025-01-02 03:55:24

您正在该方法的范围内创建 songsArray,您需要将其添加为类变量,如下所示:

MyApplication.h

@interface MyApplication {
    NSArray *songsArray;
}

MyApplication.m

- (void)applicationDidFinishLaunching… {
    songsArray = [[root nodesForXPath:@".//dict/dict/dict" error:nil] retain];
}

- (void)dealloc {
    [songsArray release];
    songsArray = nil;
}

请注意,您还需要保留它,以便当NSAutoReleasePool耗尽时数组不会自动释放。

You are creating songsArray in the scope of the method, you need to add this as a class variable as follows:

MyApplication.h

@interface MyApplication {
    NSArray *songsArray;
}

MyApplication.m

- (void)applicationDidFinishLaunching… {
    songsArray = [[root nodesForXPath:@".//dict/dict/dict" error:nil] retain];
}

- (void)dealloc {
    [songsArray release];
    songsArray = nil;
}

Note, you will also need to retain it so the array is not release automatically when the NSAutoReleasePool is drained.

深府石板幽径 2025-01-02 03:55:24

您已将 songsArray 声明为本地变量,它在 applicationDidFinishLaunching 外部不可见。

如果您已经在标头中声明了变量,则只需分配它:

-(void)applicationDidFinishLaunching... {
    songsArray = [root nodesForXPath:@".//dict/dict/dict" error:nil];

内存管理注意事项:
如果您不使用 ARC,您还需要retain您的数组,否则它将在applicationDidFinishLaunching结束时自动释放,并且您的变量将指向已释放的内存,你的应用程序将会崩溃。

并且不要忘记在 deallocrelease它(同样,如果您不在 ARC 区域)。

You have declared songsArray as a local variable, it's not visible outside applicationDidFinishLaunching.

If you have already declared your variable in header, you need to just assign it:

-(void)applicationDidFinishLaunching... {
    songsArray = [root nodesForXPath:@".//dict/dict/dict" error:nil];

Note on memory management:
If you are not using ARC, you also need to retain your array, otherwise it will be autoreleased at the end of applicationDidFinishLaunching, and your variable will be pointing to a deallocated memory, and your app will crash.

And don't forget to release it in dealloc (again, if you are not in ARC land).

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