在iOS中向NSMutableArray添加NS​​MutableDictionary时崩溃?

时间:2014-12-08 09:14:19

标签: ios objective-c nsmutablearray firebase nsmutabledictionary

我试图将一些数据保存到fire base中。所以格式就像是将内容添加到NSMutableDictionary中然后将其添加到NSMutableArray并发送到FireBase。这是第一次它在我尝试添加第二个内容时工作了与以下消息崩溃

2014-12-08 14:36:43.388 ChangeText[1634:576618] -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x17023e580
2014-12-08 14:36:43.392 ChangeText[1634:576618] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x17023e580'
*** First throw call stack:
(0x184db259c 0x1954c00e4 0x184db9664 0x184db6418 0x184cbab6c 0x1000fce74 0x189598d34 0x189581e48 0x1895986d0 0x18959835c 0x1895918b0 0x189564fa8 0x189803f58 0x189563510 0x184d6a9ec 0x184d69c90 0x184d67d40 0x184c950a4 0x18de3f5a4 0x1895ca3c0 0x1000fd7ec 0x195b2ea08)
libc++abi.dylib: terminating with uncaught exception of type NSException

我的代码

viewDidLoad中

  arr=[[NSMutableArray alloc]init];
    Firebase *fire=[[Firebase alloc]initWithUrl:@"http://del.firebaseio.com/users"];
    [fire observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
       // self.status.text=snapshot.value;
        NSLog(@"snapshot%@",snapshot.value);
        arr=[snapshot.value copy];

        NSLog(@"copied%@",arr);


    }];

按钮将值添加到Firebase

// arr=[[NSMutableArray alloc]init];

    dict=[[NSMutableDictionary alloc]init];
    [dict setValue:@"test1" forKey:@"username"];
    [dict setValue:@"test2" forKey:@"password"];
    [dict setValue:@"test3" forKey:@"profilepic"];
    [dict setValue:@"test4" forKey:@"profile"];
    [dict setValue:@"test5" forKey:@"details"];
    [arr addObject:dict];
    NSLog(@"added new elements%@",arr);

    Firebase *f=[[Firebase alloc]initWithUrl:@"http://del.firebaseio.com/users"];
    [f setValue:arr ];

我的控制台记录

2014-12-08 14:36:32.017 ChangeText[1634:576618] snapshot(
        {
        details = test5;
        password = test2;
        profile = test4;
        profilepic = test3;
        username = test1;
    }
)
2014-12-08 14:36:32.019 ChangeText[1634:576618] copied(
        {
        details = test5;
        password = test2;
        profile = test4;
        profilepic = test3;
        username = test1;
    }
)
2014-12-08 14:36:43.388 ChangeText[1634:576618] -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x17023e580
2014-12-08 14:36:43.392 ChangeText[1634:576618] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x17023e580'
*** First throw call stack:
(0x184db259c 0x1954c00e4 0x184db9664 0x184db6418 0x184cbab6c 0x1000fce74 0x189598d34 0x189581e48 0x1895986d0 0x18959835c 0x1895918b0 0x189564fa8 0x189803f58 0x189563510 0x184d6a9ec 0x184d69c90 0x184d67d40 0x184c950a4 0x18de3f5a4 0x1895ca3c0 0x1000fd7ec 0x195b2ea08)
libc++abi.dylib: terminating with uncaught exception of type NSException

请帮我解决此错误

2 个答案:

答案 0 :(得分:2)

在这种情况下,

[arr addObject:dict];正在创建崩溃,因为arr是不可变的。

arr=[snapshot.value copy];为您提供了一个不可变的对象副本。你可以试试

arr=[snapshot.value mutableCopy];

答案 1 :(得分:0)

arr = [snapshot.value copy];

以上行将NSArray类型实例分配给您的arr对象。这是您尝试在数组中添加对象的原因,它会崩溃,因为当前实例的类型为NSArray,而不是NSMutableArray。用以下行替换上面的行:

[arr addObjectsFromArray:snapshot.value];