gpt4 book ai didi

swift - public init中的问号是什么意思?(coder aDecoder : NSCoder)?

转载 作者:搜寻专家 更新时间:2023-10-30 22:12:54 25 4
gpt4 key购买 nike

我不认为 public init?(coder aDecoder: NSCoder) 中的问号是关于可选项的。此外,当我覆盖它时,我发现我根本不需要写问号。

那么这到底是什么意思呢?

---更新---

下面的评论帮助我弄明白了。它被称为“可失败的初始化程序”。另一个可以使概念更容易理解的例子是 UIFont 的便利初始化,因为 UIFont 可能不存在。

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

最佳答案

它被称为可失败初始化器。在 The Swift Programming Language 一书中,它描述为

“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.”

检查 Swift Docs 中的“失败的初始化程序”部分

关于swift - public init中的问号是什么意思?(coder aDecoder : NSCoder)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35549152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com