类型的价值' GTLDriveFile'没有会员' downloadUrl'

时间:2016-02-16 16:36:10

标签: ios swift google-drive-api

我正在关注此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
}

1 个答案:

答案 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