cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Mixing Swift and Objective C

Mixing Swift and Objective C

joejjank
Helpful | Level 6
Go to solution

I have an application that was first developed for the IPhone and then added support for mac os.  Much of this has been developed in Objective C - this is all working fine.  As I add new functions I like to do the new development in Swift. Is this possible? In my AppDelegate (Objective C) I save the accessToken - when I try to use it to list a folder I get error -999. Here is a sample:

let client = DropboxClient(accessToken: obj.accesstoken)

 

            _ = client.files.listFolder(path: "/Backup")

                .response { response, error in

                if let result = response {

                    print("Folder contents:")

                    for entry in result.entries {

                        print(entry.name)

                    }

                } else if let error = error

                {

                    print(error)

                }

            }

 

any help would greatly be appreciated.

1 Accepted Solution

Accepted Solutions

joejjank
Helpful | Level 6
Go to solution

Ok - thanks for the feedback. I have some common Objective C in a class that I can call from Swift so that works fine. So until I convert the entire application to swift I can live with that restriction. 

View solution in original post

4 Replies 4

Greg-DB
Dropbox Staff
Go to solution

First, to clarify, do you have the official Dropbox API v2 Objective-C SDK installed in your project, and that's what you're trying to call from the Swift code?

If so, and you're able to run this code and get a -999 error, I suspect you actually have that loaded successfully already. (I would expect it to fail in a different way if it was a matter of bridging between Objective-C and Swift.)

This error message may instead just be indicating that your "client" variable is going out of scope before the API call can succeed. Can you check on that? Here's a thread with the same error as an example:

https://www.dropboxforum.com/t5/API-Support-Feedback/999-quot-cancelled-quot-Error/m-p/192322#M24501 

joejjank
Helpful | Level 6
Go to solution

Thank Greg - I have both the Objective C and Swift API V2 loaded - is that a problem? The function is in the viewDidLoad - it purpose is the identify the files in my Backup directory on Dropbox and display them in a NSTableView. I am a little confused what I need to use - can you call the Objective C API from Swift?

 

override func viewDidLoad()

    {

        super.viewDidLoad()

        let obj:DataClassOS = DataClassOS.getInstance()

        let client = DropboxClient(accessToken: obj.accesstoken)

 

            _ = client.files.listFolder(path: "/Backup")

                .response { response, error in

                if let result = response {

                    print("Folder contents:")

                    for entry in result.entries {

                        print(entry.name)

                    }

                } else if let error = error

                {

                    print(error)

                }

            }

    }

Greg-DB
Dropbox Staff
Go to solution

The SDKs weren't designed with the intention that a single app would use both of them, but I can't say off hand if doing so would or wouldn't cause issues. I don't believe we've explicitly tested that.

You generally can import Objective-C into Swift though. Here's Apple's documentation for doing so:

https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_objective-...

And here's an old Dropbox blog post that covered how to do so, albeit for an older, now retired SDK:

https://blogs.dropbox.com/developers/2014/09/swift-apps-with-dropbox/

(For the API v2 Objective-C SDK, the import statement is instead "#import <ObjectiveDropboxOfficial/ObjectiveDropboxOfficial.h>".)

If you can get that to work for your app, that might be better, for the sake of simplicity. 

In any case, based on the additional code/context you shared here, I think the specific issue you're seeing is just due to the "client" variable going out of scope, since it's only defined in "viewDidLoad". Try moving that out of "viewDidLoad" so that it won't get cleaned up before the API call completes. 

joejjank
Helpful | Level 6
Go to solution

Ok - thanks for the feedback. I have some common Objective C in a class that I can call from Swift so that works fine. So until I convert the entire application to swift I can live with that restriction. 

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    joejjank Helpful | Level 6
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?