objective-c NSArray initWithObjects:未加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/688715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:18:07  来源:igfitidea点击:

NSArray initWithObjects: not loading

iphoneobjective-c

提问by 4thSpace

As a test, I'm doing the following as the first line in applicationDidFinishLaunching:

作为测试,我在 applicationDidFinishLaunching 的第一行执行以下操作:

NSArray *list=[[NSArray alloc] initWithObjects:@"Andy",@"Erik",@"Aaron",nil];

After the line runs, I have zero objects in the array. I'm doing this further down the code path but wanted to eliminate any influence to make sure my syntax is correct. I get the same results with NSMutableArray. In the debugger, I'm mousing over the array name to see if it has any values.

线路运行后,数组中的对象为零。我在代码路径的更深处这样做,但希望消除任何影响以确保我的语法正确。我用 NSMutableArray 得到相同的结果。在调试器中,我将鼠标悬停在数组名称上以查看它是否有任何值。

When I mouse over count in the following line, I see "varaible optimized awa... Summary":

当我将鼠标悬停在以下行中的计数上时,我看到“变量优化了...摘要”:

int count = [list count];

Any suggestions why the array doesn't fill and why count isn't giving back an integer?

任何建议为什么数组没有填充以及为什么计数不返回整数?

回答by Himadri Choudhury

What is happening is that you are not using the 'count' variable and the compiler is optimizing this out.

发生的情况是您没有使用“计数”变量,编译器正在优化它。

1) Try running in debug instead of release mode. Debug mode will not optimize stuff out, so you should see the count variable give the right result with a mouse over in the debugger. You can change the active configuration to debug from release by clicking on the drop down bar on the top left of xcode.

1) 尝试在调试而不是发布模式下运行。调试模式不会优化内容,因此您应该看到计数变量在调试器中悬停鼠标时给出正确的结果。您可以通过单击 xcode 左上角的下拉栏将活动配置更改为从发布进行调试。

2) Use the 'count' variable in your code. Note that in the release build things can happen out of order, if that's what the compiler wants to do. For example if you have your count initialization statement somewhere, and you use 'count' further down in your code, 'count' may not get set until before where 'count' is actually used.

2) 在代码中使用“count”变量。请注意,在发布版本中,如果编译器想要这样做,事情可能会发生乱序。例如,如果您在某处有您的计数初始化语句,并且您在代码中进一步使用了 'count',则在实际使用 'count' 之前,'count' 可能不会设置。