gpt4 book ai didi

swift - 如何让返回等待计算完成

转载 作者:行者123 更新时间:2023-11-30 13:37:07 24 4
gpt4 key购买 nike

预先感谢您的帮助。

我正在尝试从 Dropbox 中的 CSV 下载数据。我能够成功下载数据,但现在我正在尝试从这些进程中封装/创建函数。也就是说,我想创建一个函数,在其中下载 CSV 并将数据作为字符串返回。

我的问题是这样的:我的函数在从 Dropbox 下载完成之前返回数据变量。因此它不会返回任何内容。即,我将首先看到注释“返回 allData 变量”,然后是“转换为 allData 变量”。我需要以某种方式扭转这一局面。

我看到 2 个选项:1)弄清楚如何工作这个异步位(我是 Swift 的新手,为了更好地理解它,我一直在网上搜索),或者 2)以某种方式构建 return 调用仅在数据完全下载后执行。

对于如何继续前进,你们有什么建议吗?我在下面包含了我的代码。

func downloadFile () -> [[String:String]] {

var allData = [[String:String]]()

if let client = Dropbox.authorizedClient {

let destination : (NSURL, NSHTTPURLResponse) -> NSURL = { temporaryURL, response in

let fileManager = NSFileManager.defaultManager()
let directoryURL = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]

// generate a unique name for this file in case we've seen it before

let UUID = NSUUID().UUIDString

let pathComponent = "\(UUID)-\(response.suggestedFilename!)"

print ("The path component is \(pathComponent)")

return directoryURL.URLByAppendingPathComponent(pathComponent)

}

//Option 1: Create some bit of asynchronous code here?
client.files.download(path: "/Master Test.csv", destination: destination).response { response, error in

if let (metadata, url) = response {

print("*** Download file ***")

let data = NSData(contentsOfURL: url)
let dlString = NSString(data: data!, encoding: NSUTF8StringEncoding)

let a = CSwiftV(String: String(dlString!))

allData = a.keyedRows!

print ("converted to allData variable")

//Option 2: Somehow embed the return call here?

} else {

print (error!)

}
}
}

print ("returned allData variable")

return allData

}

最佳答案

我建议向您的函数添加一个completionHandler。您可以在 swift 文档中了解有关闭包的更多信息。 https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html

func downloadFile (completion: (data: [[String:String]]?, error: NSError?) -> ()) -> (){

var allData = [[String:String]]()

if let client = Dropbox.authorizedClient {

let destination : (NSURL, NSHTTPURLResponse) -> NSURL = { temporaryURL, response in

let fileManager = NSFileManager.defaultManager()
let directoryURL = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]

// generate a unique name for this file in case we've seen it before

let UUID = NSUUID().UUIDString

let pathComponent = "\(UUID)-\(response.suggestedFilename!)"

print ("The path component is \(pathComponent)")

return directoryURL.URLByAppendingPathComponent(pathComponent)

}

//Option 1: Create some bit of asynchronous code here?
client.files.download(path: "/Master Test.csv", destination: destination).response { response, error in

if let (metadata, url) = response {

print("*** Download file ***")

let data = NSData(contentsOfURL: url)
let dlString = NSString(data: data!, encoding: NSUTF8StringEncoding)

let a = CSwiftV(String: String(dlString!))

allData = a.keyedRows!

print ("converted to allData variable")

//Option 2: Somehow embed the return call here?
completion(data: allData, error: nil)

} else {

print (error!)
completion(data: nil, error: error)

}
}
}

print ("returned allData variable")

return allData

}

函数调用

 downloadFile(){
//start your loader here
dropboxData in
//remove here
if(dropboxData.error == nil){
print(dropboxData.data)
}else{
print(dropboxData.error)
}

}

关于swift - 如何让返回等待计算完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35953862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com