我对API已经完全陌生了,并且正在关注this tutorial将Dropbox的API应用到我正在构建的iPhone应用中。该教程是用早期版本的swift编写的,所以我的问题可能是翻译中的一个问题。我在教程中相当远,它说我应该能够运行应用程序,但是当我这样做时,它会因致命错误而崩溃。
这里是ViewController.swift代码
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tblFiles: UITableView!
@IBOutlet weak var bbiConnect: UIBarButtonItem!
@IBOutlet weak var progressBar: UIProgressView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tblFiles.delegate = self
tblFiles.dataSource = self
progressBar.isHidden = true
NotificationCenter.default.addObserver(self, selector: Selector(("handleDidLinkNotification:")), name: NSNotification.Name(rawValue: "didLinkToDropboxAccountNotification"), object: nil)
func handleDidLinkNotification(notification: NSNotification)
{
bbiConnect.title = "Disconnect"
}
if DBSession.shared().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.shared().isLinked() {
DBSession.shared().link(from: self)
}
else {
DBSession.shared().unlinkAll()
bbiConnect.title = "Connect"
}
}
@IBAction func performAction(_ sender: AnyObject) {
}
@IBAction func reloadFiles(_ sender: AnyObject) {
}
// MARK: UITableview method implementation
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 0.0
}
}
AppDelegate.swift代码
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
@nonobjc func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let appKey = "XXXXX" // Set your own app key value here.
let appSecret = "XXXXX" // Set your own app secret value here.
let dropboxSession = DBSession(appKey: appKey, appSecret: appSecret, root: kDBRootAppFolder)
DBSession.setShared(dropboxSession)
return true
}
@nonobjc func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
if DBSession.shared().handleOpen(url as URL!) {
if DBSession.shared().isLinked() {
return true
}
}
return false
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
我在这里超出了我的深度。谢谢你的帮助!