Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

ios - Cannot return items in a string array

I'm very new to Parse and Swift and I have this project I am working on and I am trying to create a search bar that displays all the items from the key "names" from my Parse database.

I have created this function that is supposed to take all the names and return them in a string array. But instead, the array never gets filled and all I get as a return is [].

class Offices {
    var name: String
    var phone: String
    var location: String
    
    init(name: String = "def_name", phone: String = "def_phone", location: String = "def_location") {
        self.name = name
        self.phone = phone
        self.location = location
    }
    func retrieveName() -> [String] {
        var models = [String]()
        let queries = PFQuery(className: "Directory")
        queries.findObjectsInBackground { (object, error) in
            if let error = error {
                // The query failed
                print(error.localizedDescription)
            } else if let object = object {
                // The query succeeded with a matching result
                for i in object{
                    models.append(i["name"] as? String ?? self.name)
                }
                
            } else {
                // The query succeeded but no matching result was found
            }
            
        }
        return models
    }
question from:https://stackoverflow.com/questions/65875640/cannot-return-items-in-a-string-array

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

1 Reply

0 votes
by (71.8m points)

findObjectsInBackground method is asynchronous. So you should change retrieveName function as below:

class Offices {
    var name: String
    var phone: String
    var location: String
    
    init(name: String = "def_name", phone: String = "def_phone", location: String = "def_location") {
        self.name = name
        self.phone = phone
        self.location = location
        
        // I call retrieveName here for example. You can call it where you want.
        retrieveName() { (success, models) in
            if success {
                print(models)
            } else {
                print("unsuceess")
            }
        }
    }

    func retrieveName(completion: @escaping (_ success: Bool, _ models: [String]) -> Void) {
        var models = [String]()
        let queries = PFQuery(className: "Directory")
        queries.findObjectsInBackground { (object, error) in
            if let error = error {
                // The query failed
                print(error.localizedDescription)
                completion(false, [])
            } else if let object = object {
                // The query succeeded with a matching result
                for i in object{
                    models.append(i["name"] as? String ?? self.name)
                }
                completion(true, models)
            } else {
                completion(true, [])
                // The query succeeded but no matching result was found
            }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...