从 .plist 加载 NSArray 时的 EXC_BAD_ACCESS

发布于 2024-09-10 10:39:28 字数 1029 浏览 3 评论 0原文

我有一个由 [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 技术交流群。

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

发布评论

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

评论(2

滥情稳全场 2024-09-17 10:39:28

您需要先初始化数组。使用 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"]];.

九命猫 2024-09-17 10:39:28

您的 NSLog 语句不正确。 %@ 格式字符串用于打印 Objective-C 对象,但 [testArray count] 返回一个普通 int,并且当 NSLog 尝试向结果发送一条 -description 消息来打印它时,这会导致崩溃。您需要使用 %d 来打印整数值。

NSLog(@"count = %d",[testArray count]);

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.

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