即使已填充数组,Swift函数也会返回一个空数组-SWIFT

时间:2018-08-26 08:47:47

标签: ios swift alamofire

我有一个返回对象数组的函数,并且我正在使用alamofire进行网络调用。我的函数运行正常,从控制台可以看到我正在填充数组。我的问题是返回此数组。如果将返回值放在'// END OF COMPLETION HANDLER'之前,则会出现错误:

std::string

如果将返回值放在完成处理程序之后,即使控制台清楚地表明正在填充我的数组,也总是返回一个空数组。有人可以请教吗?

 Unexpected non-void return value in void function

CONSOLE(当返回值位于完成处理程序之后时):

 func returnFolderContents(foldername: String) -> [ScanObj] {

    // GET API KEY
    guard let API_KEY = UserDefaults.standard.string(forKey: "api_key") else{return []}
    var arrayScanObj: [ScanObj] = []

    let param: [String:String] = ["getscans":"forfolder", "folderName": foldername, "api_key": API_KEY]
    Alamofire.request(GET_FOLDER_URL, method: .post, parameters: param).responseJSON { response in

        if let json = response.result.value{

            // CASTING JSON
            let jsonObj = json as! [String:Any]
            let code = jsonObj["code"] as! Int
            print("code: \(code)")

            if(code != 104){
                // NO RECORDS FOUND
                self.showPopUp(msg: "Sign In Fail")
                return
            }

            DispatchQueue.main.async{
                let content = jsonObj["arrayScanObj"] as! [[String: Any]]
                for elem in content{

                    let scanId = elem["scanId"] as! String
                    let pdfPath = elem["pdfPath"] as! String
                    let thumbWebPath = elem["thumbWebPath"] as! String
                    let arrayHashTags = elem["arrayHashTags"] as! [String]

                    // URL = S3_DOMAIN + '/' + BUCKETNAME + '/' + path;
                    let thumbNailURL = self.S3_DOMAIN + "/" + self.BUCKETNAME + "/" + thumbWebPath
                    let pdfURL = self.S3_DOMAIN + "/" + self.BUCKETNAME + "/" + pdfPath

                    // CONSTRUCT HASHTAG STRING FOR DISPLAY
                    var stringHashTags = ""
                    for tag in arrayHashTags{
                        stringHashTags = stringHashTags + "#" + tag + " "
                    }

                    // CREATE ScanObj AND
                    let scanObj = ScanObj.init(thumbNailURL: thumbNailURL, PdfURL: pdfURL, hashTags: stringHashTags, scanId: scanId)
                    arrayScanObj.append(scanObj)
                    print("arrayScanObj.count PRE: \(arrayScanObj.count)")

                }// END FOR-LOOP
                 // return arrayScanObj - I have tried this also [error: 'Unexpected non-void return value in void function']
            }
        }else{
           // return arrayScanObj - I have tried this also [error: 'Unexpected non-void return value in void function']
        }
    }// END OF COMPLETION HANDLER

    print("arrayScanObj.count: \(arrayScanObj.count)")
    return arrayScanObj
}

0 个答案:

没有答案