我试图将一些数据保存到 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/