Search code examples
iosnsurlsessionnsurlsessiondownloadtask

Silence unimplemented protocol method warning for NSURLSessionDownloadTask


I'm trying to figure out how to suppress a compiler warning for a delegate method that I have no purpose of using. I know I could have an empty method body, but I would still like to find a way to not do this, so it is less code in my source.

I saw this answer here: Dynamic forwarding: suppress Incomplete Implementation warning

But that seems to overcomplicate the matter. Is there any way of just having a one liner in the header file of my ViewController.h so I never see this warning?

I appreciate any help offered.

For the record, I'm wanting to silence the warning for this method:

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
                                  didResumeAtOffset:(int64_t)fileOffset
                                 expectedTotalBytes:(int64_t)expectedTotalBytes;

Solution

  • Missing REQUIRED protocol method warnings should never be silenced.

    If it were an optional method, it'd be marked as such and wouldn't cause a warning. As it's required, you're just asking for a crash by not including it.

    If the object being delegated attempts to call this method on its delegate and you've simply suppressed the warning rather than appropriately including it (as it's marked required), your app will crash with an unrecognized selector exception.

    If you want to silence the warning, include the method and just leave it empty if you truly don't want to do anything if/when this is called.

    For example:

    - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes {};
    

    One liner.