简体   繁体   中英

Objective-C - Missing sentinel in method dispatch?

I am trying to override the init method for a UIAlertView and customize it and I am getting the following warning? "Missing sentinel in method dispatch".

What does this warning mean, and how can I resolve it?

- (id)initWithTitle:(NSString *)title 
            message:(NSString *)message 
           delegate:(id)delegate 
  cancelButtonTitle:(NSString *)cancelButtonTitle 
  otherButtonTitles:(NSString *)otherButtonTitles, ...
{
    if (self = [super initWithTitle:title 
                            message:message 
                           delegate:delegate 
                  cancelButtonTitle:cancelButtonTitle 
                  otherButtonTitles:otherButtonTitles]) 
    {
        // Customize alertView
    }

    return  self;
}

The problem seems to be, that initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles: variadic, ie, takes an arbitrary number of arguments after the last parameter. When you call the base class' method, you have two problems:

  • you are not passing all arguments along, that the caller of your version supplied, and
  • you are not passing the final nil after the last argument (which I presume is, what the compiler is complaining about).

As others have already noted, UIAlertView is not intended to be subclassed. Consider trying another approach (using convenience constructor functions/methods instead).

Take a look at the UIAlertView documentation , in particular:

Subclassing Notes

The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

This means overriding the initWithTitle method isn't going to be useful. You'll need to try another approach - perhaps if you share what you're trying to achieve people might be able to suggest an alternative (for example, using a category).

It's warning because you're not putting a nil (the sentinel) at the end of the variadic method call. But, you're out of luck with overriding a variadic method anyway. See this question for more.

This is because you can't subclass UIAlertView . From the docs :

Subclassing Notes

The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM