protocol textingprotocol : class {
func didEnterText (text:String)
}
class secondViewController: UIViewController {
weak var delegate:textingprotocol?
@IBOutlet weak var txtField: UITextField?
@IBAction func dismissButton(sender: UIButton) {
delegate!.didEnterText(txtField?.text) // A: doesn't work
delegate!.didEnterText(txtField?.text!) // B: doesn't work
delegate!.didEnterText((txtField?.text)!) // C: works
}
A:我是否还没有进行可选链接,并且该行仅在文本具有值时才有效,否则它将优雅地失败?然而它给出了:
可选类型“字符串?”的值 未拆封;你的意思是用'!' 或者 '?'?
B:即使我得到上述编译器错误,我确实解开了它,但它仍然不满意它希望它像?
C!
它不起作用
为什么 C 有效,但 B 无效?没有比 C 行更清洁的方法吗?C线看起来很不吸引人。