2016-08-06 4 views
1

Я пытаюсь сохранить изображения в Dropbox. Когда я использую эту строку: self.dbRestClient.uploadFile(uploadFilename, toPath: destinationPath, withParentRev: nil, fromPath: sourcePath)Значение типа 'NSObject ->() -> ViewController' не имеет имени 'dbRestClient'

Ошибка:

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


} 

Я не знаю, почему это происходит. Есть идеи?

+0

Возможно, это связано с тем, что «let uploadTextFileAction» находится вне любой функции. Попробуйте переместить его внутри viewDidLoad только для того, чтобы узнать, компилируется ли он. –

+0

@MikeTaverne Это делает трюк. Благодарю. Пожалуйста, добавьте, что ответ –

+0

рад, что это сработало, но я не уверен, почему, поэтому я не чувствую, что действительно ответил на вопрос. –

ответ

0

Измените свои константы, которые получают 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) 
} 
Смежные вопросы