LondonAppDev
LondonAppDev

Reputation: 9633

Unable to call method from for loop

I have an issue that is strange to me (being new to Objective-C), but probably obvious to the experts.

So I have a ViewController with a method that will add items to a NSMutableArray which is linked to a ListView. The idea is that it will populate by calling a WebService URL using AFNetworking.

The NSMutable array is defined like this:

@property NSMutableArray *newsItems;

I then made a method that adds an object to the array:

- (void)addNewsItemWithId:(NSInteger)newsId withMessage:(NSString *)message withDate:(NSString *)date {
    SCWNewsItem *item = [[SCWNewsItem alloc] init];
    item.id = newsId;
    item.date = date;
    item.preview = message;
    [self.newsItems addObject:item];
}

Then I have another method called loadInitialData which will load the data into the array.

If I test it like this:

- (void)loadInitialData {
    [self addNewsItemWithId:1 withMessage:@"A first message" withDate:@"2014-04-17"];
    [self addNewsItemWithId:2 withMessage:@"A second message" withDate:@"2014-04-17"];
}

It will work fine. I can run and view the two messages in my ListView.

However if I try to do this, it doesn't add anything:

- (void)loadInitialData {

    NSLog(@"URL: %@", URL_GET_NEWS_ITEMS);

    NSString *token = scwData.authToken;
    if (token == nil) {
        [self returnToLogin];
    }
    else {
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        [manager.requestSerializer setValue:[NSString stringWithFormat:@"Token %@", token] forHTTPHeaderField:@"Authorization"];

        [manager GET:URL_GET_NEWS_ITEMS parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            for (NSDictionary *arr in responseObject) {
                NSInteger itemId = [[arr valueForKey:@"id"] intValue];
                NSString *name = [arr valueForKey:@"author_name"];
                NSString *date = [arr valueForKeyPath:@"date_posted"];
                NSString *message = [arr valueForKey:@"message"];

                NSLog(@"itemId: %i", itemId); 

                [self addNewsItemWithId:itemId withMessage:message withDate:date];
            }

        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Failed: %@", operation.responseObject);
        }];
    }
}

I can see from using NSLog that the loop is definitely being iterated and each attribute in the JSON array is being outputted to my log, however it just won't add any items to my ListView.

Really appreciate if someone could educate me as to what is happening here.

Thanks, Mark

Upvotes: 0

Views: 75

Answers (2)

SanjayPathak
SanjayPathak

Reputation: 121

Have you initialized self.newsItems like self.newsItems = [[NSMutableArray alloc] init]; in viewDidLoad or you may try in loadInitialData too. Please check Other things looks good. Hope this helps. Let me know the output array or array count.

Upvotes: 1

Kostiantyn Koval
Kostiantyn Koval

Reputation: 8483

Create a mutable array.

- (void)loadInitialData {
   self.newsItems = [NSMutableArray new];
   //Rest of your code here

Upvotes: 0

Related Questions