0

I'm trying to assign data from a JSON object into an NSMutableArray. It's working when I try to add the data into an object, but not when I assign it to an array. Here is the code. The offending lines are in the middle of the code, separated out from the code which adds data to an object.

- (NSMutableArray *)cards {
    NSMutableArray *cards;
    Users *obj=[Users getInstance];

    NSString *getDataURL =[[NSString alloc] initWithFormat: @""];
    NSURL * url = [NSURL URLWithString:getDataURL];

    NSData * data = [NSData dataWithContentsOfURL:url];

    NSError* error;
    _jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    _savingsArray = [[NSMutableArray alloc] init];

    for(int i = 0; i < _jsonArray.count; i++)
    {
        NSString * sID = [[_jsonArray objectAtIndex:i] objectForKey:@"savingsID"];
        NSString * sName = [[_jsonArray objectAtIndex:i] objectForKey:@"name"];
        NSString * sDate = [[_jsonArray objectAtIndex:i] objectForKey:@"startDate"];
        NSString * eDate = [[_jsonArray objectAtIndex:i] objectForKey:@"endDate"];
        NSString * sGoal = [[_jsonArray objectAtIndex:i] objectForKey:@"goal"];
        NSString * sSaved = [[_jsonArray objectAtIndex:i] objectForKey:@"saved"];
        NSString * sPerDay = [[_jsonArray objectAtIndex:i] objectForKey:@"amountPerDay"];
        NSString * sUser = [[_jsonArray objectAtIndex:i] objectForKey:@"user"];

       [cards addObject:@{@"name" :[[_jsonArray objectAtIndex:i] objectForKey:@"name"]}];
       _cards = [NSMutableArray arrayWithArray:cards];

        [_savingsArray addObject:[[Savings alloc] initWithSavingsID: sID andName:  sName andStartDate: sDate andEndDate: eDate andGoal: sGoal andSaved: sSaved andAmountPerDay: sPerDay andUser:sUser]];
    }

    NSLog(@"%lu", (unsigned long)_cards.count);
    return _cards;
}

When I count the array, it is returning a 0 value, when it should in fact be 2.

Any ideas?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
spogebob92
  • 1,474
  • 4
  • 23
  • 32

0 Answers0