-1

大家好,我是 Xcode/iOS 开发的新手

我试图将 json 数据添加到可变数组中,这会导致应用程序崩溃:( 到目前为止,这是我的代码:

if(! [defaults objectForKey:@"Person1"])
    [defaults setObject:[PersonsFromSearch objectAtIndex:index] forKey:@"Person1"];
else
{
    NSMutableArray *Array = [[NSMutableArray alloc]init];
    id object = [defaults objectForKey:@"Person1"];
    Array = [object isKindOfClass:[NSArray class]] ? object : @[object];
    [Array addObject:[PersonsFromSearch objectAtIndex:index]];//crash here :((
    [Array moveObjectFromIndex:[Array count] toIndex:0];

}

崩溃转储:

* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSArrayI addObject:]:无法识别的选择器发送到实例 0xcb4d380”*第一次抛出调用堆栈:

这里有什么问题?你能帮我解决这个问题吗

数组包含这个(Json?)

{
   Address = "\U05d3\U05e8\U05da \U05d4\U05e9\U05dc\U05d5\U05dd 53";
   CellPhone = "052-3275381";
   EMail = "editor@pc.co.il";
   EnglishPerson = "Yehuda Konfortes";
   FaceBookLink = "";
   Fax1 = "03-7330703";
   Fax2 = "";
   FileNAme = "100050.jpg";
   HomeEMail = "";
   HomeFax = "";
   HomePhone1 = "";
   HomePhone2 = "";
   PersonID = 100050;
   PersonName = "\U05d9\U05d4\U05d5\U05d3\U05d4   \U05e7\U05d5\U05e0\U05e4\U05d5\U05e8\U05d8\U05e1";
   Phone1 = "03-7330733";
   Phone2 = "";
   ZipCode = "";
 }
4

2 回答 2

1

[defaults objectForKey:@"Person1"];返回字典但不返回数组。所以你不能使用addObject方法。

UPD 您可以通过使用单个对象创建数组来解决此崩溃。这是更新的代码:

NSMutableArray *Array = [[NSMutableArray alloc]init];
id object = [defaults objectForKey:@"Person1"];
Array = [object isKindOfClass:[NSArray class]] ? [object mutableCopy] : [@[object] mutableCopy];
[Array addObject:[PersonsFromSearch objectAtIndex:index]];//crash here :((
[Array moveObjectFromIndex:[Array count] toIndex:0];
于 2013-11-10T09:24:06.430 回答
0

NSMutableArray *_arr = [[NSMutableArray alloc]init]; NSDictionary *results = PASS JSON 值

[_arr 添加对象:[结果对象forkey:@“地址”]];[_arr 添加对象:[results objectforkey:@“CellPhone”]];

于 2013-11-10T09:32:52.307 回答