I load the data in the loading screen (viewDidLoad). All attempts to save the received data are unsuccessful: if you add via addObject, the array remains empty (displays nil), although there are data that we are trying to add — exist (output data); id object — stores all the data in full (it can be output) but as soon as we exit the body of this request, the data disappears (the testIdData class variable is empty after the request body, and in the data body). In general, a silly question, but how can I get this data out of the body? JSON input

- (void)viewDidLoad { [super viewDidLoad]; tableViewData = [NSMutableArray new]; // создаем запрос NSURL *url = [NSURL URLWithString:lineURLresult]; // URL NSURLSessionDataTask *downloadTask = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { id object = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error]; { if ([object isKindOfClass:[NSDictionary class]] && error == nil){ //id object2 = [tableViewData objectAtIndex:0]; id object = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error]; [_tableViewData addObject:object]; NSLog(@"%@",tableViewData); // Скриншот результата 1 } } }]; [downloadTask resume]; NSLog(@"%@",tableViewData); // Скриншот результата 2 

Variables of the viewController class in which the request is located

 @interface ViewControllerTable : UIViewController <UITableViewDelegate, UITableViewDataSource>{ } @property (strong, nonatomic)NSMutableArray *tableViewData; 

Screenshot 1 Screenshot 1

Screenshot 2

Screenshot 2

    2 answers 2

    The problem is that you are not initializing the array. Before adding elements to an array, you must initialize it: tableViewData = [NSMutableArray new];

    • tried to add via addObject: object and via addObject: data, now inside the body tableViewData has data, outside - an array is empty (no longer nil, but empty brackets) - StriBog
    • one
      Where do you initialize the array? In the completionHandler block? - Vitali Eller
    • inside viewDidLoad (screen loading) - StriBog
    • one
      Alternatively, you can try this: when declaring a variable, add __block and you will have __block NSMutableArray * tableViewData; - Vitali Eller
    • one
      The block is executed asynchronously, it is possible that the information in the log is output before the block is completed. Check the array value in ViewDidAppear - Vitali Eller

    Is the variable declared via @property? Set the strong flag instead of weak.

    The weak attribute is usually used only for delegates, since every time it enters, it nullifies the chain of references to the object, and this does not happen with the strong attribute, it indicates that it is a strong link, the chain does not collapse and the variable is not nil'yatsya.

    • Code changed, did not help, added screenshots - StriBog
    • one
      @StriBog, that's right, the response block from dataTaskWithURL is executed after logging (screenshot 2), at the time of its output the data is not received from the server, that is, first you will see a screenshot 2 in the log, then screenshot 1. You should work on the array inside the block response dataTaskWithURL - German Polyansky