1

我创建了这个 api 端点,它发回关于一个人的数据,我正试图从我的应用程序到达那个端点。我能够连接到端点,但在调用该方法后它返回 nil。

我尝试使用块语句来收集该数据并返回该数据。我还创建了一个模型,当有响应时存储该数据,您将在下面看到,但由于它是一个异步任务,所以我返回的数据似乎仍然为零。我似乎没有捕获任何数据。

- (NSString *) verifyCode:(NSString*) code{
    NSString* ID = self.potentialPerson[@"ID"];

    [self validateInput:code :ID];//nil upon return
    return self->inputModel.CodeResponse;//returns nil
}

- (void) validateInput:(NSString *)code :(NSString *) ID {
    NSString *URL = [[inputModel api] stringByAppendingFormat:@"action=validate&code=%@&inputID=%@",
                          code, ID];
    NSLog(@"%@", URL);

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URL]];
    NSURLSession *sesh = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [sesh dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if([data length] >0 && error == nil) {
            NSLog(@"DATA WAS RECEIVED");

            NSMutableDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

            self->inputModel.CodeResponse = [result objectForKey:@"messages"];
            //This outputs to console with the data I want
            NSLog(@"message: %@", self->inputModel.CodeResponse);
        }
        else {
            NSLog(@"Data Error = %@", error);
        }}];
    [dataTask resume];//When it gets here the data in CodeResponse is nil

    return;
}

编辑自上述代码发布以来我所做的更改。我遵循了 Rob 的建议,并尝试遵循如何从 NSURLSessionDataTask 获取 NSDictionary

输出到控制台时,我的存储为空


- (NSString *) verifyCode:(NSString*) code{

    NSString* ID = self.potentialPerson[@"ID"];
    NSString *URL = [[inputModel api] stringByAppendingFormat:@"action=validate&code=%@&inputID=%@",
                          code, ID];

    NSURLSessionDataTask *storage = [self validateTypedCode:URL withDictionary: ^(NSDictionary *data) {
        self.CodeResponse = data;
        NSLog(@"message %@",data);
    }];
    NSLog(@"message %@",[self.CodeResponse objectForKey:@"message"]);
}

- (NSURLSessionDataTask *) validateInput:(NSString *)URL withDictionary: (void (^)(NSDictionary* data)) completionHandler {
    NSURLRequest *request = [NSURLRequest  requestWithURL:[NSURL URLWithString:URL]];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request
                                            completionHandler:
                                  ^(NSData *data, NSURLResponse *response, NSError *error) {
                                      NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data
                                                                                              options:0
                                                                                                error:NULL];
                                      if (completionHandler)
                                          dispatch_async(dispatch_get_main_queue(), ^{
                                              completionHandler(result);
                                          });
                                  }];
    [task resume];
    return task;
}


4

0 回答 0