gpt4 book ai didi

swift - class func 是否可以返回 class 本身?

转载 作者:行者123 更新时间:2023-11-28 13:55:17 29 4
gpt4 key购买 nike

类函数是否可以返回类本身? currentUser 函数有什么用?谢谢

class FUser: FObject {
class func currentUser() -> FUser {
if let dictionary = UserDefaults.standard.object(forKey: "CurrentUser") as? [String: Any] {
return FUser(path: "User", dictionary: dictionary)
}
return FUser(path: "User")
}

class FObject: NSObject {
init(path: String, subpath: String?) {
super.init()
pathX = path
subpathX = subpath
}

convenience init(path: String) {
self.init(path: path, subpath: nil)
}
}

最佳答案

如果您指的是单例对象,那么可以这样做:

class ClassA {

static let shared = ClassA()

private init() {
// Init
}
}

因为 init 是私有(private)的,所以只有一个实例。

关于swift - class func 是否可以返回 class 本身?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53967804/

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