ios - 在 iOS 中将 NSMutableDictionary 添加到 NSMutableArray 时崩溃?

标签 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

我的代码

查看加载

  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

请帮我解决这个错误

最佳答案

[arr addObject:dict]; 在这种情况下造成了崩溃,因为 arr 是不可变的。

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

arr=[snapshot.value mutableCopy];

关于ios - 在 iOS 中将 NSMutableDictionary 添加到 NSMutableArray 时崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27354722/

相关文章:

objective-c - 从一个 NSMutableArray 中删除包含在另一个数组中的元素

objective-c - 如何缓存弹出 Controller ?

ios - iOS快速枚举时需要修改预加载数据的NSMutableArray

iphone - 不使用网页或 safari 的 Twitter 身份验证?

iphone - 在iphone中从多个数组中添加数组中的对象

objective-c - ICMP 和 iPhone SDK

iphone sqlite undefined symbol 错误

objective-c - NSMutableArray 计算对象的出现次数,然后重新排列数组

ios - 键盘处理就像在 iOS 7 中的消息应用程序中一样

ios - Developer/SDKs/iPhoneSimulator9.1.sdk/usr/lib/system/libcommonCrypto.dylib文件错误