从 .plist 加载 NSArray 时的 EXC_BAD_ACCESS
我有一个由 [NSMutableArray writeToFile] 从 NSMutableArray 编写的 plist。当尝试使用以下代码加载相同的 plist 时:
NSArray *testArray = [NSArray arrayWithContentsOfFile:[self pathForDataFile:@"reportingSpeicher.plist"]];
NSLog(@"count = %@",[testArray count]);
我在 count 或在 testArray 上尝试的任何其他操作上都遇到了错误的访问。但是:
NSLog(@"testArray = %@", testArray);
正确返回:
testArray = (
{
benutzername = "t.h";
datum = "2010-07-15";
dauerInStunden = 1;
phasenName = "Projektsteuerung,32";
projektName = "projekt AG,23";
soapSpeicher = {
PasswortAsMD5 = someMD5sum;
benutzername = "t.h";
datum = "2010-07-15";
dauerInStunden = 1;
phasenid = 32;
projektid = 23;
taetigkeit = whateveryoudid;
};
taetigkeit = whateveryoudid;
} )
我猜测要么涉及一些基本的内存管理,要么返回的类型以某种方式损坏/不是 NSArray。这三行确实应该足够简单 - 我只是无法让它工作。我将不胜感激任何帮助!
I have a plist written from a NSMutableArray by [NSMutableArray writeToFile]. When trying to load that same plist with the following code:
NSArray *testArray = [NSArray arrayWithContentsOfFile:[self pathForDataFile:@"reportingSpeicher.plist"]];
NSLog(@"count = %@",[testArray count]);
I get bad access on the count or on any other operation I try on testArray. BUT:
NSLog(@"testArray = %@", testArray);
correctly returns:
testArray = (
{
benutzername = "t.h";
datum = "2010-07-15";
dauerInStunden = 1;
phasenName = "Projektsteuerung,32";
projektName = "projekt AG,23";
soapSpeicher = {
PasswortAsMD5 = someMD5sum;
benutzername = "t.h";
datum = "2010-07-15";
dauerInStunden = 1;
phasenid = 32;
projektid = 23;
taetigkeit = whateveryoudid;
};
taetigkeit = whateveryoudid;
} )
I'm guessing there is either some basic memory management involved or the type returned is somehow corrupted/not an NSArray. Those three lines should really be simple enough - I just can't get it to work. I'd appreciate any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要先初始化数组。使用 NSArray *testArray = [[NSArray alloc] initWithContentsOfFile:[self pathForDataFile:@"reportingSpiicher.plist"]];。
You need to initialise the array first. Use
NSArray *testArray = [[NSArray alloc] initWithContentsOfFile:[self pathForDataFile:@"reportingSpeicher.plist"]];
.您的 NSLog 语句不正确。 %@ 格式字符串用于打印 Objective-C 对象,但 [testArray count] 返回一个普通 int,并且当 NSLog 尝试向结果发送一条
-description
消息来打印它时,这会导致崩溃。您需要使用 %d 来打印整数值。Your NSLog statement is incorrect. The %@ format string is used when printing out an Objective-C object, but [testArray count] returns a plain int, and when NSLog tries to send the result a
-description
message to print it out, that results in a crash. You'll want to use %d instead to print out the integer value.