簡體   English   中英

如何用nullable編寫完成塊?

[英]How can I write completion block with nullable?

當我用nil調用此方法時,應用程序崩潰,但我想知道如何使用nullable編寫它。

CRASH

 [KPTaxnoteApiSaveHandler saveEntryWithUuid:uuid completion:nil];

 [KPTaxnoteApiSaveHandler saveEntryWithUuid:uuid completion:^(NSError *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);
}

這將處理完成為零的情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM