无法将nil传递给UIAlertView init方法

时间:2015-02-16 00:23:30

标签: ios cocoa-touch swift

我正在尝试创建一个没有消息的UIAlertView。根据文档,该init方法的声明是:

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

然而,当我这样称呼那个方法时:

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

我收到错误“Type'String'不符合协议'NilLiteralConvertible'”。

消息参数是一个可选的字符串,为什么我不能将nil传递给它?

1 个答案:

答案 0 :(得分:3)

你没有打电话

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

您正在致电

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

因为您的otherButtonTitles参数,message字段声明为String,因此不是可选的。