[英]NSMutableArray in appdelegate - EXEC_BAD_ACCESS
I'm trying to use a global array in the appdelegate as a quick fix for a demo I'm building. 我正在尝试在appdelegate中使用全局数组作为我正在构建的演示的快速修复。 The relevant code for this
有关的代码
.h file has the declaration .h文件具有声明
{
{
NSMutableArray *trnEntered;NSMutableArray * trnEntered;
}}
@property (nonatomic, retain) NSMutableArray *trnEntered
@属性(非原子,保留)NSMutableArray * trnEntered
.m file has the following code - .m文件具有以下代码-
trnEntered = [[NSMutableArray alloc] init];
trnEntered = [[NSMutableArray alloc] init];
NSLog(@"%@",[trnEntered count]);NSLog(@“%@”,[trnEntered count]); // prints null.
//打印null。
[trnEntered addObject:@"1"];[trnEntered addObject:@“ 1”];
NSLog(@"%@",[trnEntered count]);NSLog(@“%@”,[trnEntered count]); // exec bad access.
//执行错误的访问权限。
Not sure where I'm going wrong here. 不知道我在哪里错了。 Looks fairly straight forward.
看起来相当简单。
Thanks for the help in advance, 我在这里先向您的帮助表示感谢,
Teja. 特雅
There seems to be a problem with your code: 您的代码似乎有问题:
NSLog(@"%@",[trnEntered count]); // prints null.
[trnEntered addObject:@"1"];
NSLog(@"%@",[trnEntered count]); // exec bad access.
Both calls to NSLog are trying to print an NSUInteger
as an Objective-C object. 两次对NSLog的调用都试图将
NSUInteger
打印为Objective-C对象。 This is going to cause a problem. 这将导致问题。 You should use
NSLog(@"%d", [trnEntered count]);
您应该使用
NSLog(@"%d", [trnEntered count]);
I would suggest that you read more on format specifiers , there is a lot of useful information. 我建议您阅读有关格式说明符的更多信息,这里有很多有用的信息。
As a little more information: the second NSlog
is trying to print the description
of the NSObject that the code claims is located at memory location 0x00000001. 更多信息:第二个
NSlog
正在尝试打印代码声明位于内存位置0x00000001的NSObject的description
。
第一个NSLog不应打印'null',因为您的init方式有问题,请尝试执行此操作。
trnEntered = [[NSMutableArray alloc] initWithCapacity:1];
trnEntered = [[NSMutableArray alloc] init];
NSLog(@"%@",[trnEntered count]); // prints null.
[trnEntered addObject:@"1"];
NSLog(@"%@",[trnEntered count]); // exec bad access.
The reason is because you're using the formatter string wrong. 原因是因为您使用的格式化字符串错误。 You're trying to use an integer as a string pointer, which will of course lead to a segmentation fault (or EXC_BAD_ACCESS in apple terms).
您正在尝试使用整数作为字符串指针,这当然会导致分段错误(或苹果术语EXC_BAD_ACCESS)。 Use %d to print an integer, which is what [trnEntered count] returns.
使用%d打印整数,即[trnEntered count]返回的整数。
NSLog
默认情况下以String格式打印ans,因此,如果要打印整数值,则必须使用%d
及其值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.