键入' String'不符合协议' NilLiteralConvertiblle'

时间:2014-09-19 22:38:15

标签: ios ios7 swift

这行代码:

var errorView = UIAlertView(title: errorTitle, message: errorString, delegate:self, cancelButtonTitle: "Cancel", otherButtonTitles: "OK", nil)

从Objective-C代码重写:

 UIAlertView *errorView =
        [[UIAlertView alloc] initWithTitle:errorTitle
            message:errorString delegate:self cancelButtonTitle:nil
            otherButtonTitles:@"OK", nil];

这给了我这个错误:

  

类型'String'不符合协议'NilLiteralConvertiblle'

我可以通过不把nil放到最后来修复它,但我只是不知道为什么,有人知道答案吗?

1 个答案:

答案 0 :(得分:2)

Variadic Parameter需要{-1}}在Objective-C中终止,但在Swift中没有。

Swift方法签名:

nil

Objective-C方法签名:

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

从技术上讲,它是- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /*<UIAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION ,它是Swift中的可变参数。