Swift 如何使用init参数调用静态成员
我有一个名为WareHouse的类和一个必需的init() 我想用下面的例子来称呼它,但我不知道如何编程。 有什么建议吗 仓库(数据库名称:“谷歌服务信息”).share.sign(电子邮件:xxxx,密码:XXX) 下面是我的示例代码Swift 如何使用init参数调用静态成员,swift,Swift,我有一个名为WareHouse的类和一个必需的init() 我想用下面的例子来称呼它,但我不知道如何编程。 有什么建议吗 仓库(数据库名称:“谷歌服务信息”).share.sign(电子邮件:xxxx,密码:XXX) 下面是我的示例代码 class WareHouse{ public static let shared = WareHouse(databaseName: "GoogleService-Info-WarehouseDev") .... required init(databas
class WareHouse{
public static let shared = WareHouse(databaseName: "GoogleService-Info-WarehouseDev")
....
required init(databaseName:String) {
let filePath = Bundle.main.path(forResource: databaseName, ofType: "plist")
if let filePath = filePath {
let fileopts = FirebaseOptions(contentsOfFile: filePath)
FirebaseApp.configure(name: "Warehouse", options: fileopts!)
app = FirebaseApp.app(name: "Warehouse")
}
}
func signIn(withEmail:String, withPassword:String){
}
}
您可以像下面那样设计仓库
class WareHouse {
private static var wareHouses: [WareHouse] = []
public static func of(name: String) -> WareHouse {
if let wareHouse = wareHouses.first(where: { (wareHouse) -> Bool in
wareHouse.databaseName == name
}) {
return wareHouse
} else {
let wareHouse = WareHouse(databaseName: name)
wareHouses.append(wareHouse)
return wareHouse
}
}
private let databaseName: String
required init(databaseName:String) {
self.databaseName = databaseName
}
public func yourFunction() {
}
}
您可以在WareHouse类中调用函数,如下所示:
WareHouse.of(name: "").yourFunction()
通过这种方式,您可以使用数据库名称维护仓库类的不同共享实例。您要求同时执行两个相反的操作。您可以调用
WareHouse.shared
或调用WareHouse(databaseName:)
。你不能两者都做!(事实上,第一个为你呼叫第二个。)@matt嗨,matt,那么我如何才能呼叫相同的实例?e、 g.仓库(“GoogleService Info WarehouseDev”).signIn()然后是仓库(“GoogleService Info WarehouseDev”).fetchData()