0
因此,在一個塊中,我得到並解析了一個看起來像這樣的JSON對象(任何時候你看到「< ...」都假設數據正確)只有有問題的對象)之一:NSArray與嵌套對象在內存中「丟失」對象
{
asset = {
<...>
};
<...>
sections = (
{
<...>
fields = (
);
items = (
);
},
{
<...>
fields = (
);
items = (
);
},
{
<...>
fields = (
);
items = (
{
<...>
properties = {
title = "We welcome guests...";
};
},
{
<...>
properties = {
title = "More text";
};
},
{
<...>
properties = {
title = "Opportunity Insert...";
};
}
);
},
{
<...>
fields = (
{
<...>
values = (
Private,
Public
);
},
{
<...>
values = (
);
},
{
<...>
values = (
);
}
);
items = (
);
},
{
<...>
fields = (
{
<...>
values = (
);
},
{
<...>
values = (
);
}
);
items = (
);
}
);
}
的代碼得到這個(使用AFNetworking):
NSMutableURLRequest* req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:AppsURL([[NSUserDefaults standardUserDefaults] objectForKey:kAuthToken])]];
AFHTTPRequestOperation* op = [[AFHTTPRequestOperation alloc] initWithRequest:req];
[op setResponseSerializer:[[AFJSONResponseSerializer alloc] init]];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
if (responseObject != nil&&[responseObject isKindOfClass:[NSDictionary class]]) {
NSDictionary* dict = (NSDictionary*)responseObject;
if (dict[@"apps"]&&[dict[@"apps"] isKindOfClass:[NSArray class]]) {
apps = dict[@"apps"];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
return;
}
}
NSLog(@"Error Loading apps:%@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error:%@",error);
NSLog(@"Response:%@",operation.response.description);
}];
[op start];
此時一切都加載到內存中,可以正常使用。我將其設置爲類級別的NSArray,然後使用該NSArray填充表格。當用戶從表格中選擇一個項目後嘗試檢索數據時,問題就出現了。然後我用食指和拉該對象從NSArray的發現是看起來像這樣(請注意一個事實,即所有的項目和領域陣列現在是空的):
{
asset = {
<...>
};
<...>
sections = (
{
<...>
fields = (
);
items = (
);
},
{
<...>
fields = (
);
items = (
);
},
{
<...>
fields = (
);
items = (
);
},
{
<...>
fields = (
);
items = (
);
}
);
}
代碼選擇:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row < apps.count) {
//the output of this is what I copy and pasted
NSLog(@"app data to be cleaned up:%@ \r app data sent:%@",apps[indexPath.row],((NSDictionary*)apps[indexPath.row]).mutableCopy);
//I originally thought the problem was here, but the output above proved me wrong
NSMutableDictionary *app = [Functions recurseDictionaryForNull:((NSDictionary*)apps[indexPath.row]).mutableCopy];
[[NSUserDefaults standardUserDefaults] setObject:app
forKey:@"app"];
MainMenuViewController* mainMenu = (MainMenuViewController*)[self.storyboard instantiateViewControllerWithIdentifier:@"mainMenu"];
[self.navigationController setViewControllers:@[mainMenu] animated:YES];// reset nav stack
}
}
我有點失落...任何想法什麼可以做到這一點,以及如何解決它?
沒有足夠的信息給任何人來幫助你。你應該提供創建和訪問這個數組的相關代碼。 – Dima
添加代碼,請不要在有人得到回覆的機會之前進行投票...有點粗魯。 –
不幸的是,其他人低估了你。 – Dima