undeclared selector warning 警告处理

场景:使用自定义的SDWebImage缓存处理,需要调用一个SDWebImage中的一个私有方法sd_setImage:imageData:basedOnClassOrViaCustomSetImageBlock:

{
    // 使用默认的图片
    SEL sel = @selector(sd_setImage:imageData:basedOnClassOrViaCustomSetImageBlock:);
    dispatch_main_async_safe(^{
        if ([self respondsToSelector:sel]) {
            UIImage* image = [UIImage imageNamed:@"image"];
            NSData* data = UIImagePNGRepresentation(image);
            ((void (*)(id, SEL, UIImage*, NSData*, id))(void *) objc_msgSend)((id)self, sel, image, data, nil);
        }
    });
}

编译遇到一个警告

warning: undeclared selector 'sd_setImage:imageData:basedOnClassOrViaCustomSetImageBlock:' [-Wundeclared-selector]
            SEL sel = @selector(sd_setImage:imageData:basedOnClassOrViaCustomSetImageBlock:);
                       ^
1 warning generated.
  • 解决方案1
{
    // 使用默认的图片
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
    SEL sel = @selector(sd_setImage:imageData:basedOnClassOrViaCustomSetImageBlock:);
#pragma clang diagnostic pop
    dispatch_main_async_safe(^{
        if ([self respondsToSelector:sel]) {
            UIImage* image = [UIImage imageNamed:@"image"];
            NSData* data = UIImagePNGRepresentation(image);
            ((void (*)(id, SEL, UIImage*, NSData*, id))(void *) objc_msgSend)((id)self, sel, image, data, nil);
        }
    });
}
  • 解决方案2
{
    // 使用默认的图片
    // 这种方式也是可以的 
    // SEL sel = NSSelectorFromString(@"sd_setImage:imageData:basedOnClassOrViaCustomSetImageBlock:");
    SEL sel = sel_registerName("sd_setImage:imageData:basedOnClassOrViaCustomSetImageBlock:");
    dispatch_main_async_safe(^{
        if ([self respondsToSelector:sel]) {
            UIImage* image = [UIImage imageNamed:@"image"];
            NSData* data = UIImagePNGRepresentation(image);
            ((void (*)(id, SEL, UIImage*, NSData*, id))(void *) objc_msgSend)((id)self, sel, image, data, nil);
        }
    });
}

引用
How to get rid of the 'undeclared selector' warning

你可能感兴趣的:(undeclared selector warning 警告处理)