V
V
Vladislav Pavlenko2016-02-25 00:30:38
Objective-C
Vladislav Pavlenko, 2016-02-25 00:30:38

How to get the number of rooms in the received data?

Hello! Today I "mastered" NSURLSession, I still don't understand much.
I can’t figure out where to get the number of rooms correctly to build a collection view.

#pragma mark - GET homes

- (void)getRequestHomes
{
    __weak DashboardViewController *wSelf = self;
    
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
    
    NSURL *url = [NSURL URLWithString:@"http://api.plugmee.com/api/Homes"];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:20.0];
    
    [request addValue:[NSString stringWithFormat:@"bearer %@", [self.userDefaults objectForKey:@"token"]] forHTTPHeaderField:@"Authorization"];
    
    [request setHTTPMethod:@"GET"];
    
    NSURLSessionDataTask *getDataTask = [session dataTaskWithRequest:request
                                                   completionHandler:^(NSData * _Nullable data,
                                                                       NSURLResponse * _Nullable response,
                                                                       NSError * _Nullable error)
                                         {
                                             NSLog(@"method getRequestHomes");
                                             
                                             wSelf.receivedData =  forHTTPHeaderField:@"Authorization"];
    
    [request setHTTPMethod:@"GET"];
    
    NSURLSessionDataTask *getDataTask = [session dataTaskWithRequest:request
                                                   completionHandler:^(NSData * _Nullable data,
                                                                       NSURLResponse * _Nullable response,
                                                                       NSError * _Nullable error)
                                         {
                                             NSLog(@"method getRequestRooms");
                                             
                                             wSelf.receivedData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                             NSLog(@"полученные данные: %@", wSelf.receivedData);
                                             
                                             wSelf.resultResponse = [NSString stringWithFormat:@"%@", response];
                                             NSLog(@"ответ сервера: %@", wSelf.resultResponse);
                                             
                                             wSelf.roomsJSON = [RoomsModel arrayOfModelsFromData:data error:nil];
                                             if (wSelf.roomsJSON)
                                                 NSLog(@"Data rooms saved");
                                         }];
    
    [getDataTask resume];
}

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
V
Vladislav Pavlenko, 2016-02-25
@pavlenkovs

https://toster.ru/answer?answer_id=766664

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question