1

我从 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];
                    });
                }
            }

        });

}
4

1 回答 1

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 
}
于 2013-06-12T14:52:10.900 回答