Answer the question
In order to leave comments, you need to log in
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question