Getting null value after adding objects to customClass

Posted by Brian Stacks on Stack Overflow See other posts from Stack Overflow or by Brian Stacks
Published on 2014-06-06T15:22:34Z Indexed on 2014/06/06 15:24 UTC
Read the original article Hit count: 466

Filed under:
|
|
|

Ok here's my code first viewController.h

@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>
{
NSMutableArray *twitterObjects;
}

@property (strong, nonatomic) IBOutlet UICollectionView *myCollectionView;

Here is my viewController.m

//
//  ViewController.m
//  MDF2p2
//
//  Created by Brian Stacks on 6/5/14.
//  Copyright (c) 2014 Brian Stacks. All rights reserved.
//

#import "ViewController.h"
// add accounts framework to code
#import <Accounts/Accounts.h>
// add social frameworks
#import <Social/Social.h>
#import "TwitterCustomObject.h"
#import "CustomCell.h"
#import "DetailViewController.h"

@interface ViewController ()

@end

@implementation ViewController

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    //CustomCell * cell = (CustomCell*)sender;
    //NSIndexPath *indexPath = [_myCollectionView indexPathForCell:cell];
    // setting an id for view controller
    DetailViewController *detailViewcontroller = segue.destinationViewController;
    //TwitterCustomObject *newCustomClass = [twitterObjects objectAtIndex:indexPath.row];
    if (detailViewcontroller != nil) {
        // setting the custom customClass object
        //detailViewcontroller.myNewCurrentClass = newCustomClass;
    }    
}


- (void)viewDidLoad
{
    twitterObjects = [[NSMutableArray alloc]init];
    [super viewDidLoad];
    [self twitterAPIcall];
    // Do any additional setup after loading the view, typically from a nib.

}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 100;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];
    // initiate celli
    CustomCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];
    // add objects to cell
    if (cell != nil) {
        //TwitterCustomObject *newCustomClass = [twitterObjects objectAtIndex:indexPath.row];

        //[cell refreshCell:newCustomClass.userName userImage:newCustomClass.userImage];
        [cell refreshCell:@"Brian" userImage:[UIImage imageNamed:@"love.jpg"]];

    }

    return cell;
}

-(void)twitterAPIcall
{
    //create an instance of the account store from account frameworks
    ACAccountStore *accountStore = [[ACAccountStore alloc]init];
    // make sure we have a valid object
    if (accountStore != nil)
    {
        // get the account type ex: Twitter, FAcebook info
        ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
        // make sure we have a valid object
        if (accountType != nil)
        {
            // give access to the account iformation
            [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error)
             {
                 if (granted)
                 {
                     //^^^success user gave access to account information
                     // get the info of accounts
                     NSArray *twitterAccounts = [accountStore accountsWithAccountType:accountType];
                     // make sure we have a valid object
                     if (twitterAccounts != nil)
                     {
                         //NSLog(@"Accounts: %@",twitterAccounts);
                         // get the current account information
                         ACAccount *currentAccount = [twitterAccounts objectAtIndex:0];
                         // make sure we have a valid object
                         if (currentAccount != nil)
                         {
                             //string from twitter api
                             NSString *requestString = @"https://api.twitter.com/1.1/friends/list.json";
                             // request the data from the request screen call
                             SLRequest *myRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:[NSURL URLWithString:requestString]  parameters:nil];
                             // must authenticate request
                             [myRequest setAccount:currentAccount];
                             // perform the request named myRequest
                             [myRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
                              {
                                  // check to make sure there are no errors and we have a good http:request of 200
                                  if ((error == nil) && ([urlResponse statusCode] == 200))
                                  {
                                      // make array of dictionaries from the twitter api data using NSJSONSerialization
                                      NSArray *twitterFeed = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
                                      NSMutableArray *nameArray = [twitterFeed valueForKeyPath:@"users"];


                                      // for loop that loops through all the post
                                      for (NSInteger i =0; i<[twitterFeed count]; i++)
                                      {
                                          NSString *nameString = [nameArray valueForKeyPath:@"name"];
                                          NSString *imageString = [nameArray valueForKeyPath:@"profile_image_url"];
                                          NSLog(@"Name feed: %@",nameString);
                                          NSLog(@"Image feed: %@",imageString);

                                          // get data into my mutable array
                                          TwitterCustomObject *twitterInfo = [self createPostFromArray:[nameArray objectAtIndex:i]];
                                          //NSLog(@"Image feed: %@",twitterInfo);
                                          if (twitterInfo != nil)
                                          {
                                              [twitterObjects addObject:twitterInfo];

                                          }


                                      }

                                  }
                              }];
                         }

                     }
                 }
                 else
                 {
                     // the user didn't give access
                     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning"
                                                                     message:@"This app will only work with twitter accounts being allowed!."
                                                                    delegate:self
                                                           cancelButtonTitle:@"OK"
                                                           otherButtonTitles:nil, nil];
                     [alert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:FALSE];

                 }
             }];
        }
    }

}

-(TwitterCustomObject*)createPostFromArray:(NSArray*)postArray
{
    // create strings to catch the data in
    NSArray *userArray = [postArray valueForKeyPath:@"users"];
    NSString *myUserName = [userArray valueForKeyPath:@"name"];
    NSString *twitImageURL = [userArray valueForKeyPath:@"profile_image_url"];
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:twitImageURL]]];


    // initiate object to put the data in
    TwitterCustomObject *twitterData = [[TwitterCustomObject alloc]initWithPostInfo:myUserName myImage:image];
    NSLog(@"Name: %@",myUserName);
    return twitterData;
}
-(IBAction)done:(UIStoryboardSegue*)segue
{

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Here is my customObject class TwitterCustomClass.h

//
//  TwitterCustomObject.h
//  MDF2p2
//
//  Created by Brian Stacks on 6/5/14.
//  Copyright (c) 2014 Brian Stacks. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface TwitterCustomObject : NSObject
{

}
@property (nonatomic, readonly) NSString *userName;
@property (nonatomic, readonly) UIImage *userImage;
-(id)initWithPostInfo:(NSString*)screenName myImage:(UIImage*)myImage;
@end

TwitterCustomClass.m

//
//  TwitterCustomObject.m
//  MDF2p2
//
//  Created by Brian Stacks on 6/5/14.
//  Copyright (c) 2014 Brian Stacks. All rights reserved.
//

#import "TwitterCustomObject.h"

@implementation TwitterCustomObject

-(id)initWithPostInfo:(NSString*)screenName myImage:(UIImage*)myImage
{
    // initialize as object
    if (self = [super init])
    {
        // use the data to be passed back and forth to the tableview
        _userName = [screenName copy];
        _userImage = [myImage copy];

    }
    return self;

}
@end

The problem is I get the values in the method twitterAPIcall, I can get the names and image values or strings from the values. But in the (TwitterCustomObject*)createPostFromArray:(NSArray*)postArray method all values are coming up as null.I thought it got added with this line of code in the twitterAPIcall method [twitterObjects addObject:twitterInfo];?

© Stack Overflow or respective owner

Related posts about JSON

Related posts about ios7