使用在Xcode 7.1.1上运行的IOS 9.1。在El Capitan下。我正在按照此页面尝试通过其(可能是新的)SwiftDropbox库访问Dropbox的教程。

https://blogs.dropbox.com/developers/2015/05/try-out-swiftydropbox-the-new-swift-sdk-for-dropbox-api-v2/

起步很好,可以连接并获得授权,但是由于发布到其教程的代码无法编译而无法做更多的事情。

if let client = Dropbox.authorizedClient {

    // Get the current user's account info
    client.usersGetCurrentAccount().response { response, error in
        if let account = response {
            println("Hello \(account.name.givenName)")
        } else {
            println(error!)
        }
    }

    // List folder
    client.filesListFolder(path: "").response { response, error in
        if let result = response {
            println("Folder contents:")
            for entry in result.entries {
                println(entry.name)
            }
        } else {
            println(error!)
        }
    }

抱怨说DropboxClient没有成员“usersGetCurrentAccount”或“filesListFolder”,我认为这是某种排序方法调用{对于swift来说也是很新的}。似乎无法在Google上找到任何有用的线索吗?此代码应读/说什么?

最佳答案



谢谢smarx;你让我走上了正确的道路。这是最终代码;对他在这里提到的教程进行了两个小修复。

//  ViewController.swift

import UIKit
import SwiftyDropbox

class ViewController: UIViewController {


@IBAction func linkButtonPressed(sender: AnyObject) {
        if (Dropbox.authorizedClient == nil) {
            Dropbox.authorizeFromController(self)
        } else {
            print("User is already authorized!")
        }
}


override func viewDidLoad() {
    super.viewDidLoad()

    // Verify user is logged into Dropbox
    if let client = Dropbox.authorizedClient {

        // Get the current user's account info
        client.users.getCurrentAccount().response { response, error in
            print("*** Get current account ***")
            if let account = response {
                print("Hello \(account.name.givenName)!")
            } else {
                print(error!)
            }
        }

        // List folder
        client.files.listFolder(path: "").response { response, error in
            print("*** List folder ***")
            if let result = response {
                print("Folder contents:")
                for entry in result.entries {
                    print(entry.name)
                }
            } else {
                print(error!)
            }
        }

        // Upload a file
        let fileData = "Hello!".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
        client.files.upload(path: "/hello.txt", body: fileData!).response { response, error in
            if let metadata = response {
                print("*** Upload file ****")
                print("Uploaded file name: \(metadata.name)")
                print("Uploaded file revision: \(metadata.rev)")

                // Get file (or folder) metadata
                client.files.getMetadata(path: "/hello.txt").response { response, error in
                    print("*** Get file metadata ***")
                    if let metadata = response {
                        if let file = metadata as? Files.FileMetadata {
                            print("This is a file with path: \(file.pathLower)")
                            print("File size: \(file.size)")
                        } else if let folder = metadata as? Files.FolderMetadata {
                            print("This is a folder with path: \(folder.pathLower)")
                        }
                    } else {
                        print(error!)
                    }
                }

                // Download a file

                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!)"
                    return directoryURL.URLByAppendingPathComponent(pathComponent)
                }
                client.files.download(path: "/hello.txt", destination: destination).response { response, error in
                //client.files.download(path: "/hello.txt").response { response, error in
                    if let (metadata, url) = response {
                        print("*** Download file ***")
                        let data = NSData(contentsOfURL: url)
                        print("Downloaded file name: \(metadata.name)")
                        print("Downloaded file url: \(url)")
                        print("Downloaded file data: \(data)")
                    } else {
                        print(error!)
                    }
                }

            } else {
                print(error!)
            }
        }
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

关于ios - Dropbox usersGetCurrentAccount没有成员编译器错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34180770/

10-12 14:38