2016-08-06 62 views
1

Ich versuche, Bilder in Dropbox zu speichern. Wenn ich diese Zeile verwenden: self.dbRestClient.uploadFile(uploadFilename, toPath: destinationPath, withParentRev: nil, fromPath: sourcePath)Wert vom Typ 'NSObject ->() -> ViewController' hat kein Mitglied 'dbRestClient'

Fehler:

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

Hier ist mein Code:

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) 
    } 


} 

Ich weiß nicht, warum dies geschieht. Irgendwelche Ideen?

+0

Es könnte sein, weil die "lade uploadTextFileAction" außerhalb jeder Funktion ist. Versuchen Sie, es in viewDidLoad zu verschieben, nur um zu sehen, ob es kompiliert wird. –

+0

@MikeTaverne Das macht den Trick. Vielen Dank. Bitte fügen Sie das als Antwort –

+0

froh, dass es funktioniert hat, aber ich bin mir nicht sicher, warum, also habe ich nicht das Gefühl, dass ich die Frage wirklich beantwortet habe. –

Antwort

0

Ändern Sie Ihre Konstanten, die auf self wie self.dbRestClient zugreifen, auf lazy var. Nur faule und berechnete Eigenschaften können in ihrer Initialisierung auf self zugreifen. z.B. Der erste wird wie folgt aussehen: