Вопрос

I have written the following code but I keep on getting nil. I have tried many different variations of this but I am failing exceptionally hard.

This is what I am getting from the server. Two objects.

[{"description":"yolo.","name":"ye","id":1},{"description":"sMITH","name":"John","id":2}]

Any help would be greatly appreciated...... Thanks.

    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];
    SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
    NSArray *jsonObjects = [jsonParser objectWithData:response];
    NSMutableString *yolo = [[NSMutableString alloc] init];

            for ( int i = 0; i < [jsonObjects count]; i++ ) {
                NSDictionary *jsonDict = [jsonObjects objectAtIndex:i];
                NSString *IDID = [jsonDict objectForKey:@"id"];
                NSString *name = [jsonDict objectForKey:@"name"];

                NSLog(@"ID: %@", IDID); // THIS DISPLAYS

                [yolo appendString: IDID];    // THIS seems to be causing the new error...
                [yolo appendString:@": "];
                [yolo appendString: name];
                NSLog(@"%@", yolo);           // RETURNS NIL    
    }

EDIT:

currently my new error is...

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSDecimalNumber length]: unrecognized selector sent to instance 0x81b89f0'

Это было полезно?

Решение

Looks like your [jsonDict objectForKey:@"id"] is an NSNumber(or NSDecimalNumber) and not an NSString. You should change the line NSString *IDID = [jsonDict objectForKey:@"id"]; to,

id myObject = [jsonDict objectForKey:@"id"];
NSString *IDID = nil;

if ([myObject isKindOfClass:[NSNumber class]]) {
   IDID = [[jsonDict objectForKey:@"id"] stringValue];
} else {
   IDID = [jsonDict objectForKey:@"id"];
}

This error appeared now since earlier you were not initializing NSMutableString *yolo and you were using appendString: on a nil object. Since now it is initialized as NSMutableString *yolo = [[NSMutableString alloc] init]; it is trying to call appendString on NSMutableString object which accepts only NSString type as its inputs where as you are passing an NSNumber in it. length is a method which appendString: internally calls. So you need to change this as well.

Другие советы

You never initialize yolo, so it's just nil the whole time you're calling -appendString: on it. Try this:

NSMutableString *yolo = [NSMutableString string];

Have you tried initializing the NSMutableString?

NSMutableString *yolo = [[NSMutableString alloc] init];

It looks like you are not really checking the type of the data coming to your app via your JSON feed. This might be the case of random crashes when users actually use your app. It might be also a reason for rejection to the App Store, is such crashes happen during your App's review.

You should be checking the type of all objects you receive from JSON, before calling methods on them :)

By implementing best practices you will have a stable and usable app. Build data models to validate your data. You can also you a JSON data model framework like JSONModel: http://www.jsonmodel.com/

It's obvious from your data that "id" is not a string, but a number. Assigning a pointer to an NSString* doesn't magically convert it to an NSString*. And it's obvious from the exception that you got that some object is an NSDecimalNumber when you thought it would be an NSString.

So: IDID is an NSNumber*, and pretending it is an NSString* will lead to crashes.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top