我从json加载数据,然后将其添加到nsmutablearray,如下所示:
- (void)loadData
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
// Create array to hold dictionaries
myObject = [[NSMutableArray alloc] init];
NSData *jsonData = [NSData dataWithContentsOfURL:
[NSURL URLWithString:@"http://www.domain.com/json.php"]];
if(jsonData != nil)
{
NSError *error = nil;
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
if (error == nil){
dispatch_sync(dispatch_get_main_queue(), ^{
// values in foreach loop
for (NSMutableArray *tempArray in jsonObjects) {
[myObject addObject:tempArray];
NSSortDescriptor * sortDesc = [[NSSortDescriptor alloc] initWithKey:@"id.doubleValue" ascending:NO];
[myObject sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
[self performSelectorOnMainThread:@selector(endAnimating) withObject:nil waitUntilDone:YES];
}
});
}
}
});
}
如果我检查NSLog“tempArray
”它看起来没问题,但是如果我检查“myObject
”,则会多次添加数据。如何只将一次数据添加到我的“myObject
”数组中?
编辑:
我的JSON结果:
[{"id":"7","title":"monkey","thumb":"http:\/\/icon.s.photosight.ru\/img\/8\/e09\/5045427_thumb.jpg","url":"http:\/\/icon.s.photosight.ru\/img\/8\/e09\/5045427_large.jpg","day":"perjantai","date":"0","likes":"2","device_id":"1111","active":"1"},
{"id":"11","title":"Bukashka","thumb":"http:\/\/icon.s.photosight.ru\/img\/f\/b3b\/5078973_thumb.jpg","url":"http:\/\/icon.s.photosight.ru\/img\/f\/b3b\/5078973_large.jpg","day":"perjantai","date":"0","likes":"1","device_id":"1111","active":"1"},
{"id":"12","title":"blya","thumb":"http:\/\/icon.s.photosight.ru\/img\/f\/c1d\/5076251_thumb.jpg","url":"http:\/\/icon.s.photosight.ru\/img\/f\/c1d\/5076251_large.jpg","day":"perjantai","date":"0","likes":"1","device_id":"1111","active":"1"}]
我的NSLog(@“%@”,myObject);
2013-06-12 18:45:52.228 testApp[960:60b] (
{
active = 1;
date = 0;
day = perjantai;
"device_id" = 1111;
id = 7;
likes = 2;
thumb = "http://icon.s.photosight.ru/img/8/e09/5045427_thumb.jpg";
title = monkey;
url = "http://icon.s.photosight.ru/img/8/e09/5045427_large.jpg";
}
)
2013-06-12 18:45:52.230 testApp[960:60b] (
{
active = 1;
date = 0;
day = perjantai;
"device_id" = 1111;
id = 11;
likes = 1;
thumb = "http://icon.s.photosight.ru/img/f/b3b/5078973_thumb.jpg";
title = Bukashka;
url = "http://icon.s.photosight.ru/img/f/b3b/5078973_large.jpg";
},
{
active = 1;
date = 0;
day = perjantai;
"device_id" = 1111;
id = 7;
likes = 2;
thumb = "http://icon.s.photosight.ru/img/8/e09/5045427_thumb.jpg";
title = monkey;
url = "http://icon.s.photosight.ru/img/8/e09/5045427_large.jpg";
}
)
2013-06-12 18:45:52.237 testApp[960:60b] (
{
active = 1;
date = 0;
day = perjantai;
"device_id" = 1111;
id = 12;
likes = 1;
thumb = "http://icon.s.photosight.ru/img/f/c1d/5076251_thumb.jpg";
title = blya;
url = "http://icon.s.photosight.ru/img/f/c1d/5076251_large.jpg";
},
{
active = 1;
date = 0;
day = perjantai;
"device_id" = 1111;
id = 11;
likes = 1;
thumb = "http://icon.s.photosight.ru/img/f/b3b/5078973_thumb.jpg";
title = Bukashka;
url = "http://icon.s.photosight.ru/img/f/b3b/5078973_large.jpg";
},
{
active = 1;
date = 0;
day = perjantai;
"device_id" = 1111;
id = 7;
likes = 2;
thumb = "http://icon.s.photosight.ru/img/8/e09/5045427_thumb.jpg";
title = monkey;
url = "http://icon.s.photosight.ru/img/8/e09/5045427_large.jpg";
}
)
工作解决方案:danypata
在viewDidLoad
中myObject = [[NSMutableArray alloc] init];
然后
- (void)loadData
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSData *jsonData = [NSData dataWithContentsOfURL:
[NSURL URLWithString:@"http://www.domain.com/json.php"]];
if(jsonData != nil)
{
NSError *error = nil;
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
if (error == nil){
[myObject removeAllObjects];
for (NSMutableDictionary *tempDict in jsonObjects) {
[myObject addObject:tempDict];
}
NSSortDescriptor * sortDesc = [[NSSortDescriptor alloc] initWithKey:@"id.doubleValue" ascending:NO];
[myObject sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];
dispatch_sync(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
[self.tableView.pullToRefreshView stopAnimating];
});
}
}
});
}
答案 0 :(得分:2)
问题的一个可能原因是tempArray
数组包含相同的对象,但要回答你的问题`如何只添加一次我的" myObject"阵列"有两个简单的解决方案
一,使用containsObject:
这样的方法:
if([myObject containsObject:tempArray] == NO) {
[myObject addObject:tempArray]
}
其次,我认为更优雅使用NSMutableSet
(`NSMutableSet仅在尚未添加对象时才添加对象)。您可以像这样使用它:
NSMutableSet *set = [[NSMutableSet alloc] init];
[set addObject:tempArray];
//after you added all objects just do the following after you init your myObject
[myObject addObjectsFromArray:[set allObjects]]
修改强>
您的问题是由for
循环引起的。你没有正确提取数据,在你的JSON中你有一个字典数组而不是一个数组数组,所以你应该像这样更改for
循环:
for (NSDictionary *tempDict in jsonObjects) {
[myObject addObject:tempDict];
//other operations here
}