Reputation: 6606
I have an iOS application which parses a JSON feed provided by Google Plus. This JSON feed contains links, titles, etc for posts on a Google Plus profile.
The JSON file in certain parts has an array called "attachments" and that array has the image URL which I am parsing.
The problem is that that array is not always present, so sometime its available with the image URL and sometime its not even there.
As a result of this, when I parse for the image URL my iOS application crashes because the array it is looking for is not always present.
So how can I test to see if the array is present before deciding to parse for the image URL. I tried to make a simple if-statement, but it doesn't seem to work. Here it is:
if ([[[episodes objectAtIndex:index] valueForKey:@"object"] valueForKey:@"attachments"] == [NSNull null]) {
NSLog("No image to parse.");
}
else {
// parse image link
}
Here is the JSON file:
{
"kind": "plus#activity",
"etag": "\"9Q4kEgczRt3gWehMocqSxXqUhYo/uQcxIDsySGhlI5hMFHcxjuBhM9k\"",
"title": "",
"published": "2013-02-24T22:30:25.159Z",
"updated": "2013-02-24T22:30:25.159Z",
"id": "z13jgdihsvnytdttc23hxdz5ypfsjnzsb",
"url": "https://plus.google.com/102757352146004417544/posts/7psQLdwS2F7",
"actor": {
"id": "102757352146004417544",
"displayName": "Daniel Sadjadian",
"url": "https://plus.google.com/102757352146004417544",
"image": {
"url": "https://lh4.googleusercontent.com/-EF0LibpIsEY/AAAAAAAAAAI/AAAAAAAAALQ/2nt32bqYBtM/photo.jpg?sz=50"
}
},
"verb": "post",
"object": {
"objectType": "note",
"content": "",
"url": "https://plus.google.com/102757352146004417544/posts/7psQLdwS2F7",
"replies": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/z13jgdihsvnytdttc23hxdz5ypfsjnzsb/comments"
},
"plusoners": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/z13jgdihsvnytdttc23hxdz5ypfsjnzsb/people/plusoners"
},
"resharers": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/z13jgdihsvnytdttc23hxdz5ypfsjnzsb/people/resharers"
},
"attachments": [
{
"objectType": "video",
"displayName": "SGU - The Game Has Changed!",
"content": "Send this video to your friends to spread the word about the upcoming SyFy marathon. This very well may be our last chance to save the show. If you're in the...",
"url": "http://www.youtube.com/watch?v=WtHusm7Yzd4",
"image": {
"url": "https://lh3.googleusercontent.com/proxy/z9shhK2d39jM2P-AOLMkqP2KMG2DjipCWlcPeVPaRvHkfj-nmxkxqZfntAVMoV88ZOuroRHIvG4qs-K0SaMjQw=w506-h284-n",
"type": "image/jpeg",
"height": 284,
"width": 506
},
"embed": {
"url": "http://www.youtube.com/v/WtHusm7Yzd4?version=3&autohide=1",
"type": "application/x-shockwave-flash"
}
}
]
},
"provider": {
"title": "Google+"
},
"access": {
"kind": "plus#acl",
"description": "Public",
"items": [
{
"type": "public"
}
]
}
},
Thanks for your time, Dan.
Upvotes: 0
Views: 229
Reputation: 16032
I don't see the problem (please post your error details) but when dealing with JSON, I like to use a category IfNullThenNil
. Your code would look like:
if ( [ [ episodes[index] valueForKeyPath:@"object.attachments" ] ifNullThenNil ]
{
// parse image link
}
else
{
NSLog(@"No image to parse.\n");
}
Here's the category on NSObject
/NSNull
@implementation NSObject (IfNullThenNil)
-(id)ifNullThenNil
{
return self ;
}
@end
@implementation NSNull (IfNullThenNil)
-(id)ifNullThenNil
{
return nil ;
}
@end
Upvotes: 0
Reputation: 8501
UPDATE:
Just assign that to array and check the array
is nil
.
NSMutableArray *attachments = [[[episodes objectAtIndex:index] valueForKey:@"object"] valueForKey:@"attachments"];
if (!attachments) {
NSLog("No image to parse.");
}
This will work.
Upvotes: 1