Note that there are some explanatory texts on larger screens.

plurals
  1. PONSJSONSerialization handle returning array or dictionary
    primarykey
    data
    text
    <p>I am making a call to twitters API to load some tweets for a specific section of my app. </p> <p>A small chunk of users are reporting a crash when loading the tweets view, while the rest have no problem at all. </p> <p>I have submitted the code to Apple Tech Support and they responded letting me know that NSJSONSerialization can sometimes return a NSArray or NSDictionary.</p> <p>Obviously it will throw an error is objectAtIndex: is called on an NSDictionary object, which I believe is the culprit for all of my users. </p> <p>The partial solution is to detect if it is an Array or NSDictionary.</p> <p>Here is where I am at now:</p> <pre><code>id feedData = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&amp;jsonError]; if ([feedData isKindOfClass:[NSArray class]]) { //Is array } else if ([feedData isKindOfClass:[NSDictionary class]]) { //is dictionary } </code></pre> <p>I basically need an NSArray every single time. So in the is array block, I basically just use the feedData, but in NSDictionary, how can I convert it to an NSArray that will match the structure I need. </p> <p>Honestly the biggest issue is that I cannot see what the NSDictionary structure looks like because none of my testing devices or simulator return the NSDictionary data, they all return an NSArray. </p> <p>Here is what the entire getUserFeed method that sends the request to twitter looks like:</p> <pre><code>// Get the twitter feed NSURL *requestURL = [NSURL URLWithString:TW_API_TIMELINE]; // Set up proper parameters NSMutableDictionary *timelineParameters = [[NSMutableDictionary alloc] init]; [timelineParameters setObject:kNumTweets forKey:@"count"]; [timelineParameters setObject:@"1" forKey:@"include_entities"]; // Create the Social Request SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:requestURL parameters:timelineParameters]; postRequest.account = self.delegate.userAccount; // Perform the request [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { dispatch_async(dispatch_get_main_queue(), ^{ // Check if we reached the reate limit if ([urlResponse statusCode] == 429) { // Rate limit reached // Display an alert letting the user know we have hit the rate limit UIAlertView *twitterAlert = [[UIAlertView alloc] initWithTitle:kRateLimitTitle message:kRateLimitMessage delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [twitterAlert show]; // Stop animating the pull to refresh if it is animating [self.feedTableView.pullToRefreshView stopAnimating]; return; } // Check if there was an error if (error) { NSLog(@"Error: %@", error.localizedDescription); // Stop animating the pull to refresh if it is animating [self.feedTableView.pullToRefreshView stopAnimating]; return; } // Check if there is some response data if (responseData) { NSError *jsonError = nil; id feedData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&amp;jsonError]; if ([feedData isKindOfClass:[NSArray class]]) { //Is array NSLog(@"It's an Array"); } else if ([feedData isKindOfClass:[NSDictionary class]]) { //Is dictionary NSLog(@"It's a Dictionary"); } else { //is something else } if (!jsonError) { [self gatherTweetsFromArray:feedData]; } else { // Stop animating the pull to refresh if it is animating [self.feedTableView.pullToRefreshView stopAnimating]; // Alert the user with the error UIAlertView *twitterAlert = [[UIAlertView alloc] initWithTitle:kErrorTitle message:kErrorMessage delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [twitterAlert show]; } } else { // Stop animating the pull to refresh if it is animating [self.feedTableView.pullToRefreshView stopAnimating]; // Alert the user with the error UIAlertView *twitterAlert = [[UIAlertView alloc] initWithTitle:kErrorTitle message:kErrorMessage delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [twitterAlert show]; } }); }]; </code></pre> <p>This is a MAJOR bug and I need to squash it, so any ideas or information will be greatly appreciated! Thank you! </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload