-1

我有一个名为 WareHouse 的类和一个必需的 init() 我想像下面的示例一样调用它,但我不知道如何对其进行编程。有什么建议么?

Warehouse(databaseName:"GoogleService-Info").share.signIn(withEmail:xxxx, withPassword:XXX)

以下是我的示例代码

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){
}
}
4

1 回答 1

1

您可以像下面这样设计您的WareHouse 。

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()

这样,您可以使用数据库名称维护仓库类的不同共享实例。

于 2019-12-06T09:22:42.060 回答