我正在关注此tutorial,我试图在Swift中进行此操作。
file
对象没有downloadUrl
属性,我不明白为什么。我检查了API,它说这个属性存在,至少对于Objective-C。但是Swift存在所有其他属性,为什么不downloadUrl
?
我做了一件简单的事情:
let file: GTLDriveFile!
file.downloadUrl
和downloadUrl
以红色加下划线并显示错误:
Value of type 'GTLDriveFile' has no member 'downloadUrl'
。
这是我的代码:
private let service = GTLServiceDrive()
// Parse results and display
func displayResultWithTicket(ticket : GTLServiceTicket,
finishedWithObject response : GTLDriveFileList,
error : NSError?) {
if let error = error {
showAlert("Error", message: error.localizedDescription)
return
}
var filesString = ""
if let files = response.files where !files.isEmpty {
filesString += "Files:\n"
for file in files as! [GTLDriveFile] {
var fetcher: GTMSessionFetcher = service.fetcherService.fetcherWithURL(file.downloadUrl)
filesString += "\(file.name)\n"
}
} else {
filesString = "No files found."
}
output.text = filesString
}
答案 0 :(得分:0)
我最终做的是自己生成下载网址:
let url = "https://www.googleapis.com/drive/v2/files/" + file.identifier + "?alt=media"
let fetcher = service.fetcherService.fetcherWithURLString(url)
fetcher.beginFetchWithCompletionHandler({ (data, error) -> Void in
if error == nil {
print(data)
} else {
print("An error occurred: " + error!.localizedDescription)
}
})
另一种选择是在没有"?alt = media"并从元数据json中提取downloadUrl