无法保存AFNetworking responseObject

时间:2015-02-02 07:35:26

标签: ios objective-c afnetworking-2

一个属性:

@property (strong, nonatomic)NSDictionary *responseDict;

方法:

- (void)sendRequestDictionay:(NSDictionary *)dict ToServerWithMode:(NSInteger)mode {
    NSString *requestString = [self requestStringwithMode:mode];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];

    [manager POST:requestString parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            self.responseDict = responseObject;
            NSLog(@"JSON: %@", self.responseDict);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        self.responseDict = nil;
        NSLog(@"%@", error);
    }];
    NSLog(@"response dictionary is nil or not: %@", self.responseDict);   
}

每次第一个NSLog()都会记录JSON,但第三个NSLog只记录:

"响应字典是否为零:( null)"

似乎responseObject从未分配给self.responseDict。它与第一个NSLog相矛盾。

另一个方法在另一个类中将self.responseDict作为参数将无效。

1 个答案:

答案 0 :(得分:0)

您的问题与Returning method object from inside block

类似

该块以异步方式运行,因此它将在日志语句之后运行。你应该把日志(实际上你已经有了一个日志,所以你应该看到它),以及使用结果的代码,在块内(或从块中调用的方法)。