Reputation: 247
I am using iTunes Search APIs to return the number of users that have reviewed my current app version. Since I haven't released the app yet, I have to handle the case where the iT search API returns nothing.
Here's the pertinent code:
NSDictionary *iTunesDict = [NSJSONSerialization
JSONObjectWithData:iTunesData options:0 error:&error];
NSArray *resultCount = @[[iTunesDict valueForKey:@"resultCount"]];
NSLog(@"%@", [resultCount objectAtIndex:0]);
if ([resultCount objectAtIndex:0] == 0) {
self.numberOfReviewers = @"0";
} else {
NSArray *reviewers = @[[[iTunesDict valueForKey:@"results"] valueForKey:@"userRatingCountForCurrentVersion"]];
if ([reviewers objectAtIndex:0] == nil) {
self.numberOfReviewers = @"0";
} else {
NSString *howManyReviewed = [[[reviewers objectAtIndex:0] objectAtIndex:0] stringValue];
self.numberOfReviewers = howManyReviewed;
}
My problem centers around the first if
statement. Upon inspection, the value of...
[resultCount objectAtIndex:0]
is: (__NSCFNumber *)(long)0
does not satisfy the condition in my first if
.
What do I need to make a **(__NSCFNumber *)(long)0
**== 0
??
Upvotes: 0
Views: 58
Reputation: 2192
It's returning the data as an NSNumber
object. Use the compare:
comparison function:
if ([resultCount[0] compare:@0] == NSOrderedSame) {
...
}
else {
...
}
What this is doing is comparing the resultCount
object to an NSNumber
with the value of 0
(you can use the literal @0
to short-hand an NSNumber
as I've done above). compare:
returns one of three values:
NSOrderedDescending
NSOrderedSame
NSOrderedAscending
This reads from left to right. So if I was to use NSOrderedDescending
, this would read logically "is 0
smaller than resultCount
" (in descending order from left to right).
For further reading, check out the comparing NSNumber objects documentation.
Alternatively, because you know it's a long
, you can use the longValue
method on NSNumber:
if ([resultCount longValue] == 0) {
....
}
Upvotes: 2