有没有办法从这个函数中 return ID 值? SWIFT IOS

Is there a way to return the ID value from this function? SWIFT IOS

func runQuery() -> (String){
    appSyncClient?.fetch(query: ListTodosQuery(), cachePolicy: .returnCacheDataAndFetch) {(result, error) in
        if error != nil {
            print(error?.localizedDescription ?? "")
            return
        }
        print("Query complete.")
        result?.data?.listTodos?.items!.forEach { print(([=13=]?.name)! + " " + ([=13=]?.description)!) }
    }
    return the string
}

是AWS AMPLIFY教程代码。它只打印出数据库 ([=14=]?.name, etc...).

的结果

不过,我想return的值是[=15=]?.name

当我尝试分配或设置一个 var 以从函数中取出参数时。

要么 return 什么都没有,要么不允许我 return 一个值。

有什么想法吗?

fetch 方法异步执行。异步函数不能 return 函数 return.

的结果

这里我连接 [=12=]?.names 并使用空格 space 作为分隔符。

func runQuery(completionHandler: @escaping (String) -> Void) {
    appSyncClient?.fetch(query: ListTodosQuery(), cachePolicy: .returnCacheDataAndFetch) {(result, error) in
        if error != nil {
            print(error?.localizedDescription ?? "")
            completionHandler("")
            return
        }
        print("Query complete.")
        var sampleString = ""
        result?.data?.listTodos?.items!.forEach {
            print(([=10=]?.name)! + " " + ([=10=]?.description)!)
            sampleString += [=10=]?.name + " "
        }
        completionHandler(sampleString)
    }
}

func callQuery() {
    runQuery(completionHandler: { (result) in
        // do what you want
        print(result)
    })
}