Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

ios - Swift, seeking help defining an initializer to pass into method

I'm a relatively new coder and have this fetch method that works fine. I want to adapt it so that I can call a fetch for other collections (and their respective Structs) so I don't have to keep re-creating this same block of code with only very minor changes for each different type of data call.

I know I need to expand my parameters to being :

 func fetchMachines2 (_ userID:String, _ collection:String, _initialization:whatDataTypeDoiUse?)

I don't know how to do that last part. Using this example, how do I create/define mData outside of the method so that I can pass it in?

//rework this to accept collection, the datainit, and the


func fetchMachines2 (_ userID: String)  {
    
    
    var machines = [Machine]()
    
    
    db.collection("machines").whereField("userID", isEqualTo: userID).getDocuments { (snapshot, error) in
        
        if error == nil && snapshot != nil {
            
            for doc in snapshot!.documents {
                                  
                let mData = Machine(
                    imgURL: doc["imgURL"] as? String,
                    machineID: doc["machineID"] as? String,
                    name: doc["name"] as? String,
                    type: doc["type"] as? String,
                    userID: doc["userID"] as? String
                    )
                
                machines.append(mData)
            }

            self.delegateMFP?.receiveMachines(machines: machines)
        }
        else {
            print ("there is some error with the get documents")
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...