微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift – 问号在公共init中意味着什么?(编码器aDecoder:NSCoder)?

我不认为public init中的问号?(编码器aDecoder:NSCoder)意味着选项.此外,当我覆盖它时,我发现我根本不需要写问号.

那究竟是什么意思呢?

—更新 –

下面的评论帮助我弄明白了,它被称为“可用的初始化器”,另一个使概念更容易理解的例子是UIFont的connivence init,因为UIFont可能不存在.

public /*not inherited*/ init?(name fontName: String,size fontSize: CGFloat)

解决方法

它被称为可用的初始化程序.在“Swift编程语言”一书中,它将其描述为

“It is sometimes useful to define a class,structure,or enumeration for which initialization can fail. This failure might be triggered by invalid initialization parameter values,the absence of a required external resource,or some other condition that prevents initialization from succeeding.”

https://swift.org/documentation/TheSwiftProgrammingLanguage(Swift2.2).epub

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐