微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 如何用可空写入完成块?

当我用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);
}

这将处理完成为零的情况.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐