You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.
Forum Discussion
joejjank
6 years agoHelpful | Level 6
Mixing Swift and Objective C
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.
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.
- Greg-DBDropbox Staff
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
- joejjankHelpful | Level 6
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-DBDropbox Staff
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:
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.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
5,883 PostsLatest Activity: 6 hours agoIf you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!