1

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;
Community
  • 1
  • 1
klcjr89
  • 5,862
  • 10
  • 58
  • 91
  • Locate the protocol and the method within it and move it to the bottom under @option. – Jim Tierney Jun 28 '14 at 23:58
  • You can't modify an Apple protocol file – klcjr89 Jun 28 '14 at 23:59
  • possible duplicate of [Dynamic forwarding: suppress Incomplete Implementation warning](http://stackoverflow.com/questions/14444203/dynamic-forwarding-suppress-incomplete-implementation-warning) – nhgrif Jun 29 '14 at 00:02
  • While the linked question may be overly complicated, it is likely the only solution as failing to include required protocol methods (or failing to define declared methods as the linked question is asking about) is a really good way to get an `unrecognized selector` exception, and as such, you really, really, really shouldn't be suppressing these warnings. – nhgrif Jun 29 '14 at 00:04

1 Answers1

2

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.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
  • Asking for a crash? This is a bit ridiculous. – klcjr89 Jun 28 '14 at 23:59
  • 1
    No, it's not. If the delegated object tries calling this method and you've not included it, your app will crash with an `unrecognized selector` exception. – nhgrif Jun 28 '14 at 23:59
  • It's pretty obvious it won't be called if I never call `downloadTaskWithResumeData:`, so yes it's ridiculous. – klcjr89 Jun 29 '14 at 00:03
  • Then you're probably using the wrong class for whatever you're doing if that class defines a delegate protocol which contains required methods for which you can guarantee will never be called. But why is it so hard to just include it and leave it empty, thereby guaranteeing that you never get an `unrecognized selector` exception if you really don't want to find the right class for the job. – nhgrif Jun 29 '14 at 00:05
  • I'm using it for downloading some files so I can get the download progress through the respective delegate method `- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite`, but I have no purpose for resuming a failed task is all. – klcjr89 Jun 29 '14 at 00:06
  • Then just include it and leave it empty? Why can't you just do that? – nhgrif Jun 29 '14 at 00:06
  • You're not really adding any code. All you're adding is an absolute guarantee that you never get an `unrecognized selector` exception here, even if someone else comes in and adds code to resume a failed task. – nhgrif Jun 29 '14 at 00:11