Swift:设置协议的可选属性

Swift:设置协议的可选属性,第1张

概述如何设置协议的可选属性?例如,UITextInputTraits具有多个可选的读/写属性.当我尝试以下我得到一个编译错误(无法在’textInputTraits’中分配给’keyboardType’): func initializeTextInputTraits(textInputTraits: UITextInputTraits) { textInputTraits.keyboardTyp 如何设置协议的可选属性?例如,UITextinputTraits具有多个可选的读/写属性.当我尝试以下我得到一个编译错误(无法在’textinputTraits’中分配给’keyboardType’):
func initializeTextinputTraits(textinputTraits: UITextinputTraits) {  textinputTraits.keyboardType = .Default}

通常当访问协议的可选属性时,您添加一个问号,但是在分配值时无效(错误:无法分配给此表达式的结果):

textinputTraits.keyboardType? = .Default

协议如下:

protocol UITextinputTraits : NSObjectProtocol {  optional var keyboardType: UIKeyboardType { get set }}
这是不可能的Swift(还是?).从 ADF thread引用:

Optional property requirements,and optional method requirements that return a value,will always return an optional value of the appropriate type when they are accessed or called,to reflect the fact that the optional requirement may not have been implemented.

So it’s no surprise to get optional values easily. However,setting a property requires implementation to be guaranteed.

总结

以上是内存溢出为你收集整理的Swift:设置协议的可选属性全部内容,希望文章能够帮你解决Swift:设置协议的可选属性所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1040070.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存