简体   繁体   中英

Can't pass nil to UIAlertView init method

I'm trying to create a UIAlertView that has no message. According to the documentation, the declaration for that init method is:

init(title title: String?,
          message message: String?,
         delegate delegate: AnyObject?,
cancelButtonTitle cancelButtonTitle: String?)

however when I call that method like so:

UIAlertView(
    title: "Undo",
    message: nil, 
    delegate: self, 
    cancelButtonTitle: "Cancel", 
    otherButtonTitles: "OK")

I get an error "Type 'String' does not conform to protocol 'NilLiteralConvertible'".

The message parameter is an optional String, so why can't I pass nil to it?

You're not calling

init(title: String?, message: String?, delegate: AnyObject?, cancelButtonTitle: String?)

You're calling

convenience init(title: String, message: String, delegate: UIAlertViewDelegate?, cancelButtonTitle: String?, otherButtonTitles firstButtonTitle: String, _ moreButtonTitles: String...)

because of your otherButtonTitles parameter, which has the message field declared as String , and therefore not optional.

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