这行代码:
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];
这给了我这个错误:
类型“字符串”不符合协议“NilLiteralConvertiblle”
我可以通过不加零来解决此问题,但是我不知道为什么,有人知道答案吗?
最佳答案
这是一个Variadic Parameter,在Objective-C中需要nil
终止,但在Swift中则不需要。
Swift方法签名:
init(title: String, message: String, delegate: UIAlertViewDelegate?, cancelButtonTitle: String?, otherButtonTitles firstButtonTitle: String, _ moreButtonTitles: String...)
Objective-C方法标志:
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /*<UIAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION
从技术上讲,它是
moreButtonTitles
,它是Swift中的可变参数。关于ios - 类型“字符串”不符合协议(protocol)“NilLiteralConvertiblle”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25943328/