ios - 类型 'NSObject -> () -> ViewController' 的值没有成员 'dbRestClient'

标签 ios swift xcode dropbox dropbox-api

我正在尝试将图像保存到 Dropbox。当我使用这一行时:self.dbRestClient.uploadFile(uploadFilename, toPath: destinationPath, withParentRev: nil, fromPath: sourcePath)

错误:

Value of type 'NSObject -> () -> ViewController' has no member 'dbRestClient'.

这是我的代码:

import UIKit


class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, DBRestClientDelegate {

    @IBOutlet weak var tblFiles: UITableView!

    @IBOutlet weak var bbiConnect: UIBarButtonItem!

    @IBOutlet weak var progressBar: UIProgressView!

    var dbRestClient: DBRestClient!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        tblFiles.delegate = self
        tblFiles.dataSource = self

        progressBar.hidden = true


         NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleDidLinkNotification:", name: "didLinkToDropboxAccountNotification", object: nil)

        if DBSession.sharedSession().isLinked() {
            bbiConnect.title = "Disconnect"
        }

    }

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


    // MARK: IBAction method implementation

    @IBAction func connectToDropbox(sender: AnyObject) {

        if !DBSession.sharedSession().isLinked() {
                DBSession.sharedSession().linkFromController(self)
            }
            else {
                DBSession.sharedSession().unlinkAll()
                bbiConnect.title = "Connect"
            dbRestClient = nil
            }

        if DBSession.sharedSession().isLinked() {
            bbiConnect.title = "Disconnect"
        }

    }


    @IBAction func performAction(sender: AnyObject) {

        if !DBSession.sharedSession().isLinked() {
            print("You're not connected to Dropbox")
            return
        }

        let actionSheet = UIAlertController(title: "Upload file", message: "Select file to upload", preferredStyle: UIAlertControllerStyle.ActionSheet)

        let uploadTextFileAction = UIAlertAction(title: "Upload text file", style: UIAlertActionStyle.Default) { (action) -> Void in

        }

        let uploadImageFileAction = UIAlertAction(title: "Upload image", style: UIAlertActionStyle.Default) { (action) -> Void in

        }

        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (action) -> Void in

        }

        actionSheet.addAction(uploadTextFileAction)
        actionSheet.addAction(uploadImageFileAction)
        actionSheet.addAction(cancelAction)

        presentViewController(actionSheet, animated: true, completion: nil)
    }


    @IBAction func reloadFiles(sender: AnyObject) {

    }


    // MARK: UITableview method implementation

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell()

        return cell
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 0.0
    }

    func handleDidLinkNotification(notification: NSNotification) {
        initDropboxRestClient()
        bbiConnect.title = "Disconnect"
    }



    func initDropboxRestClient() {
        dbRestClient = DBRestClient(session: DBSession.sharedSession())
        dbRestClient.delegate = self
    }




    let uploadTextFileAction = UIAlertAction(title: "Upload text file", style: UIAlertActionStyle.Default) { (action) -> Void in

        let uploadFilename = "testtext.txt"
        let sourcePath = NSBundle.mainBundle().pathForResource("testtext", ofType: "txt")
        let destinationPath = "/"

        self.dbRestClient.uploadFile(uploadFilename, toPath: destinationPath, withParentRev: nil, fromPath: sourcePath)
    }

    let uploadImageFileAction = UIAlertAction(title: "Upload image", style: UIAlertActionStyle.Default) { (action) -> Void in

        let uploadFilename = "nature.jpg"
        let sourcePath = NSBundle.mainBundle().pathForResource("nature", ofType: "jpg")
        let destinationPath = "/"

        self.dbRestClient.uploadFile(uploadFilename, toPath: destinationPath, withParentRev: nil, fromPath: sourcePath)
    }

    func restClient(client: DBRestClient!, uploadedFile destPath: String!, from srcPath: String!, metadata: DBMetadata!) {
        print("The file has been uploaded.")
        print(metadata.path)
    }

    func restClient(client: DBRestClient!, uploadFileFailedWithError error: NSError!) {
        print("File upload failed.")
        print(error.description)
    }


}

我不知道为什么会这样。有什么想法吗?

最佳答案

将访问 self 的常量(如 self.dbRestClient)更改为 lazy var。只有惰性和计算属性可以在初始化时访问 self。 例如第一个看起来像:

lazy var uploadTextFileAction: UIAlertAction = UIAlertAction(title: "Upload text file", style: UIAlertActionStyle.Default) { (action) -> Void in

   let uploadFilename = "testtext.txt"
   let sourcePath = NSBundle.mainBundle().pathForResource("testtext", ofType: "txt")
   let destinationPath = "/"

   self.dbRestClient.uploadFile(uploadFilename, toPath: destinationPath, withParentRev: nil, fromPath: sourcePath)
}

关于ios - 类型 'NSObject -> () -> ViewController' 的值没有成员 'dbRestClient',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38804691/

相关文章:

ios - 如何在任何时候(甚至在转换期间)或在可以安全地关闭 View Controller 时关闭 View Controller ?

ios - Swift 中的正则表达式,很难让它工作

xcode - Swift:协议(protocol)有真正的目的吗?

python - Kivy 在 macOS 上编译错误

ios - SwiftUI:创建自定义警报

ios - 如何检查 locationManager 是否更新了当前位置?

javascript - 为什么 rowsAffected 不返回整数

ios - Swift - 以编程方式设置 UITableViewCell 在滚动时重置

iOS Swift 如何打开位置权限弹出窗口

ios - 为什么我的 UILabel 文本在 Xcode 8.1 的 Swift3 中没有改变