-1

I try to dynamically add objects to a NSArray. 0 error appears in XCode.

However, the IOS simulator generates errors and closes.

The static version of my code works fine.

So I do not understand where does the error.

Static version :

  candyArray = [NSArray arrayWithObjects:
              [Candy candyOfCategory:@"chocolate" name:@"chocolate bar"],
              [Candy candyOfCategory:@"chocolate" name:@"chocolate chip"],
              [Candy candyOfCategory:@"chocolate" name:@"dark chocolate"],
              [Candy candyOfCategory:@"hard" name:@"lollipop"],
              [Candy candyOfCategory:@"hard" name:@"candy cane"],
              [Candy candyOfCategory:@"hard" name:@"jaw breaker"],
              [Candy candyOfCategory:@"other" name:@"caramel"],
              [Candy candyOfCategory:@"other" name:@"sour chew"],
              [Candy candyOfCategory:@"other" name:@"peanut butter cup"],
              [Candy candyOfCategory:@"other" name:@"gummi bear"], nil];

Dynamic version :

   // get JSON data from URL
   NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http:///?view=json"]];
NSURLResponse *resp = nil;
NSError *error = nil;
NSData *response = [NSURLConnection sendSynchronousRequest: theRequest returningResponse: &resp error: &error];
NSString *jsonString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; //your json string from URL request

// PARSINF JSON VALUE
NSDictionary *jsonDictionary = [jsonString JSONValue];
NSDictionary *players = [jsonDictionary objectForKey:@"user"];

// Creating mutablearray who will receive the inputs of json:
NSMutableArray *nameInsert = [[NSMutableArray alloc] init];

for (NSDictionary *player in players)
{
    NSString *item = [player objectForKey:@"name"];

    [nameInsert addObject: [Candy candyOfCategory:@"chocolate" name:item" ]];
}

candyArray = nameInsert;

If the loop contains nothing IOS simulator works.

Of course, nothing is present in the application.

sam-w
  • 7,478
  • 1
  • 47
  • 77
  • 4
    Can you at least post the errors? – WDUK Aug 23 '13 at 09:28
  • 1
    `[nameInsert addObject: [Candy candyOfCategory:@"chocolate" name:item" ]];` what's the last quote doing - is it a direct copy-paste? – sam-w Aug 23 '13 at 09:35
  • Do you use a category on `NSString`? `NSString` has no method `JSONValue`! – HAS Aug 23 '13 at 10:20
  • And please NSLog() your `players` dictionary to make sure it is not nil. – HAS Aug 23 '13 at 10:27
  • I just have the simulator's crash – John Smith Aug 23 '13 at 10:36
  • [nameInsert addObject: [Candy candyOfCategory:@"chocolate" name:item" ]]; this is my dynamic code I'm trying to make it work. But he planted the ios simulator. – John Smith Aug 23 '13 at 10:40
  • You have a stray quotation mark after `item`. This code won't compile on Xcode. Please review the code pasted in the question to make sure that it is the exact same code that you are using. Also post the iOS simulator error that you are getting. Also add an exception breakpoint to your code as explained here: http://stackoverflow.com/questions/4961770/run-stop-on-objective-c-exception-in-xcode-4 That will help your when debugging exception related crashes. – Ricardo Sanchez-Saez Aug 23 '13 at 10:52
  • Learn how to trap and display exceptions. – Hot Licks Aug 23 '13 at 14:42

1 Answers1

1

From your code, it seems that you expect players to be an array of dictionaries because you write

(for NSDictionary *player in players)

However, you initialize players as an NSDictionary rather than an array. You would need to initialize it as an array instead:

NSDictionaryNSArray *players = [jsonDictionary objectForKey:@"user"];

Mundi
  • 79,884
  • 17
  • 117
  • 140