Why is 'self' used outside of a class?

186 views Asked by At

I'm following this tutorial on GitHub on how to implement SwiftyDropbox in an iOS app. There's a point where it tells me to add this code to my ViewController

import UIKit
import SwiftyDropbox

func myButtonInControllerPressed()
{
    DropboxClientsManager.authorizeFromController(UIApplication.shared, controller: self, openURL: { (url: URL) -> Void in UIApplication.shared.openURL(url)})
}

I then get an error saying

Use of unresolved identifier 'self'

Presumably this is because I'm declaring a function outside of a class. What am I doing wrong? Does anyone know of a tutorial/sample-app that actually works and is up to date with the latest Swift and Xcode that could teach me how to use SwiftyDropbox?

1

There are 1 answers

0
Luca Angeletti On BEST ANSWER

The paragraph before that snippet does say

You can commence the auth flow by calling authorizeFromController:controller:openURL:browserAuth method in your application's view controller.

enter image description here

So it is telling you to write that snippet INSIDE your View Controller (where using self makes sense).

Here's an example

class Controller: UIViewController {

    func myButtonInControllerPressed() {
        DropboxClientsManager.authorizeFromController(UIApplication.shared,
                                                      controller: self,
                                                      openURL: { (url: URL) -> Void in
                                                        UIApplication.shared.openURL(url)
        })
    }

}