我正在尝试将图像保存到Dropbox。当我使用此行时:self.dbRestClient.uploadFile(uploadFilename, toPath: destinationPath, withParentRev: nil, fromPath: sourcePath)
错误:
类型为“NSObject->()->ViewController”的值没有成员
“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)
    }


}

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

最佳答案

将访问selflikeself.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/

10-10 19:58