gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:31:27 25 4
gpt4 key购买 nike

我正在尝试将图像保存到 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com