applicationDidFinishLaunching 变量范围问题?
只是一个简单的问题。我有一个在我的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能已在标头中声明了
songsArray
,但未在-application:didFinishLaunching:
中分配它。相反,您创建了一个具有相同名称的局部变量,该变量只会保留在范围内直到方法结束。像这样分配你的 ivar: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
,您需要将其添加为类变量,如下所示:MyApplication.h
MyApplication.m
请注意,您还需要
保留
它,以便当NSAutoReleasePool
耗尽时数组不会自动释放。You are creating
songsArray
in the scope of the method, you need to add this as a class variable as follows:MyApplication.h
MyApplication.m
Note, you will also need to
retain
it so the array is not release automatically when theNSAutoReleasePool
is drained.您已将
songsArray
声明为本地变量,它在applicationDidFinishLaunching
外部不可见。如果您已经在标头中声明了变量,则只需分配它:
内存管理注意事项:
如果您不使用 ARC,您还需要
retain
您的数组,否则它将在applicationDidFinishLaunching
结束时自动释放,并且您的变量将指向已释放的内存,你的应用程序将会崩溃。并且不要忘记在
dealloc
中release
它(同样,如果您不在 ARC 区域)。You have declared
songsArray
as a local variable, it's not visible outsideapplicationDidFinishLaunching
.If you have already declared your variable in header, you need to just assign it:
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 ofapplicationDidFinishLaunching
, and your variable will be pointing to a deallocated memory, and your app will crash.And don't forget to
release
it indealloc
(again, if you are not in ARC land).