-4

hopefully a pretty simple question. as you can see in my code below i set two NSArray in my code within the viewdidload. The problem is that i cannot access neither of these Arrays outside of the viewDidLoad. Is there a way around this that will allow me to populate my cell.textlabel.text outside of the viewDidLoad?

Code as Follows:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSURLSession *session = [NSURLSession sharedSession];

    //URLText is from previous VC see .h file.
    NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.test.com/%@/%@.json", _URLText, _URLText]] completionHandler:^(
                                                                                                                                                                                               NSData *data,
                                                                                                                                                                                               NSURLResponse *response,
                                                                                                                                                                                               NSError *error) {
        NSDictionary *vDB = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

        NSArray *amountOfV = [vechileDB valueForKeyPath:@"amount"]; //how to assign nsstring from dictionary.
        NSArray *makeOfV = [vDB valueForKeyPath:@"make"];

        NSLog(@"%@ %@", makeOfV, amountOfV);


    }];

    //Run the completed task.
    [dataTask resume];

}
Lee Sugden
  • 63
  • 8
  • 4
    Move them out of the viewDidLoad into the class definition. And read a beginner book on programming ;-) – Gruntcakes Jul 07 '16 at 22:01
  • And for the question you're going to have after you do that, see [Variable returning NULL after Block execution](http://stackoverflow.com/q/13350108) – jscs Jul 07 '16 at 22:14
  • I'm not a fan of statements like "read a beginner book". It's not helpful, and while it may be amusing to some, it certainly isn't funny to the person asking the question. TBH, it kinda feels like a form of bullying to me. :-( – ghostatron Jul 08 '16 at 01:03
  • Instead of being sarcastic sausage, it would certainly be more helpful if you suggested an actual book to read, I'm sure you never learnt overnight and everyone has to start somewhere, also conarch I completely agree with your comment. – Lee Sugden Jul 08 '16 at 05:58
  • Maybe books from the Big Nerd Ranch or Raywenderlich(new stuff mainly Swift though), Stanford iOS lectures on iTunes U are good, but you need time to watch them. Also please accept one of the answers if your problem is solved. – JingJingTao Jul 08 '16 at 09:16
  • Have a look at [Good resources for learning ObjC](http://stackoverflow.com/q/1374660). – jscs Jul 08 '16 at 17:33

2 Answers2

3

You can define the arrays as properties in your .m file, see below, then just just reference an array with self.amountOfV.

@interface YourClass ()
@property (nonatomic) NSArray *amountOfV;
@property (nonatomic) NSArray *makeOfV;
@end

@implementation YourClass 
...

Realm has a good article on property attributes, https://realm.io/news/tmi-objective-c-property-attributes/ if you're interested.

JingJingTao
  • 1,760
  • 17
  • 28
1

You can declare both Arrays as properties such as:

@property (strong, nonatomic) NSArray *amountOfV;

and call them as self.amountOfV