Ios 如何使用nullable编写完成块?
当我使用nil调用此方法时,应用程序崩溃,但我想知道如何使用nullable编写它 撞车 嗯 这是代码Ios 如何使用nullable编写完成块?,ios,objective-c,Ios,Objective C,当我使用nil调用此方法时,应用程序崩溃,但我想知道如何使用nullable编写它 撞车 嗯 这是代码 + (void)saveEntryWithUuid:(NSString *)uuid completion:(void (^ __nullable)(NSError * _Nullable error))completion { NSLog(@"saveEntryWithUuid"); Entry *entry = [Entry MR_findFirstByAttribut
+ (void)saveEntryWithUuid:(NSString *)uuid completion:(void (^ __nullable)(NSError * _Nullable error))completion {
NSLog(@"saveEntryWithUuid");
Entry *entry = [Entry MR_findFirstByAttribute:@"uuid" withValue:uuid];
NSDictionary *params = @{@"entry[uuid]":entry.uuid};
[KPTaxnoteApiSaveHandler postWithUrl:kApiUrlStringForEntry params:params completion:^(NSError *error) {
if (!error) {
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
Entry *entry = [Entry MR_findFirstByAttribute:@"uuid" withValue:uuid inContext:localContext];
entry.needSave = @NO;
}];
}
completion(error);
}];
+ (void)postWithUrl:(NSString *)urlStr params:(NSDictionary *)params completion:(nullable void (^)(NSError *_Nullable error))completion {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:urlStr parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
completion(nil);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
completion(error);
}];
坠机发生在哪里?我的第一个猜测是你需要做这样的事情:
if (completion) {
completion(nil); // Or completion(error);
}
这将处理完成为零的情况
+ (void)saveEntryWithUuid:(NSString *)uuid completion:(void (^ __nullable)(NSError * _Nullable error))completion {
NSLog(@"saveEntryWithUuid");
Entry *entry = [Entry MR_findFirstByAttribute:@"uuid" withValue:uuid];
NSDictionary *params = @{@"entry[uuid]":entry.uuid};
[KPTaxnoteApiSaveHandler postWithUrl:kApiUrlStringForEntry params:params completion:^(NSError *error) {
if (!error) {
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
Entry *entry = [Entry MR_findFirstByAttribute:@"uuid" withValue:uuid inContext:localContext];
entry.needSave = @NO;
}];
}
completion(error);
}];
+ (void)postWithUrl:(NSString *)urlStr params:(NSDictionary *)params completion:(nullable void (^)(NSError *_Nullable error))completion {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:urlStr parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
completion(nil);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
completion(error);
}];
if (completion) {
completion(nil); // Or completion(error);
}