我正在运行一个类方法,它有一个带有我想要返回的对象的完成块。但是,当我返回值时,属性都在那里,但调试器说该对象是(null)尽管在查看更多细节时有一个内存地址。有谁知道这是怎么可能的。谢谢!
我还在我的调试区域附加了一个图像,其中显示了(null)
http://cl.ly/image/1d2j0z1D0u1p
+ (void)getBoard:(NSInteger)boardId withUser:(User *)user completion:(void (^)(Board *board))compBoard {
NSDictionary* headers = @{@"Accept": @"application/json", @"Authorization": [NSString stringWithFormat:@"Bearer %@", user._token]};
[[UNIRest get:^(UNISimpleRequest *request) {
[request setUrl:[NSString stringWithFormat:@"%@/boards/%zd", REST_URL, boardId]];
[request setHeaders:headers];
}] asJsonAsync:^(UNIHTTPJsonResponse *jsonResponse, NSError *error) {
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:[jsonResponse rawBody] options:kNilOptions error:&error];
NSLog(@"Response status of boards: %ld\n%@", (long) jsonResponse.code, json);
if ( jsonResponse.code >= 200 && jsonResponse.code <= 300 ) {
NSDictionary *boardDict = [NSJSONSerialization JSONObjectWithData:[jsonResponse rawBody] options:kNilOptions error:&error];
Board *board = [[Board alloc] init];
board._courseId = [[boardDict objectForKey:@"courseId"] integerValue];
board._udid = [[boardDict objectForKey:@"boardId"] integerValue];
board._boardName = [boardDict objectForKey:@"name"];
NSDate *dateModified = [NSDate convertFromStringFromRemote:[boardDict objectForKey:@"lastModified"]];
board._dateModified = dateModified;
NSDate *dateCreated = [NSDate convertFromStringFromRemote:[boardDict objectForKey:@"createdDate"]];
board._dateCreated = dateCreated;
compBoard(board);
}
}];
}
我称之为
- (void)addBoardWithNotification:(NSNumber *)entityId {
__weak typeof(self) weakSelf = self;
[CourseViewModal getBoard:[entityId integerValue] withUser:weakSelf._currentUser completion:^(Board *board) {
Course *boardCourse = [weakSelf._currentUser getCourseForId:board._courseId];
dispatch_async(dispatch_get_main_queue(), ^{
if ( ![boardCourse doesBoardNameExist:board._boardName] ) {
[boardCourse._boards addObject:board];
[self._currentUser addBoard:board toCourseId:boardCourse._udid];
NSNotification *addBoardNotification = [NSNotification notificationWithName:AddBoardNotificationString object:self userInfo:@{@"BoardObject" : board}];
[[NSNotificationCenter defaultCenter] postNotification:addBoardNotification];
}
});
}];
}
答案 0 :(得分:0)
我尝试过您正在使用的代码流,看不到您正在使用的块设置中的任何问题 - 看起来您还有其他部分代码正在创建问题。
测试代码 - 没有问题
+(void) get:(myCompletion) compblock asJsonAsync:(myCompletion) compblock2{
//do stuff
compblock(nil);
NSDictionary *ret2 = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObject:@"compblock2"] forKeys:[NSArray arrayWithObject:@"Key"]];
compblock2(ret2);
}
+(void) myMethod:(myCompletion) compblock {
//do stuff
[ViewController get:^(NSDictionary* finished) {
if(finished){
NSLog(@"compblock %@",[finished objectForKey:@"Key"]);
}
compblock(finished);
} asJsonAsync:^(NSDictionary* finished) {
if(finished){
NSLog(@"compblock2 %@",[finished objectForKey:@"Key"]);
}
NSDictionary *ret = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObject:@"compblock2"] forKeys:[NSArray arrayWithObject:@"Key"]];
compblock(ret);
}];
}
//call
[ViewController myMethod:^(NSDictionary* finished) {
if(finished){
NSLog(@"success %@",[finished objectForKey:@"Key"]);
}else {
NSLog(@"nil");
}
}];
您可以查看以下代码吗?
+ (void)getBoard:(NSInteger)boardId withUser:(User *)user completion:(void (^)(Board *board))compBoard {
NSDictionary* headers = @{@"Accept": @"application/json", @"Authorization": [NSString stringWithFormat:@"Bearer %@", user._token]};
[[UNIRest get:^(UNISimpleRequest *request) {
[request setUrl:[NSString stringWithFormat:@"%@/boards/%zd", REST_URL, boardId]];
[request setHeaders:headers];
}] asJsonAsync:^(UNIHTTPJsonResponse *jsonResponse, NSError *error) {
Board *board = [[Board alloc] init];
compBoard(board);
}];
}