Echizzle Echizzle - 2 years ago 84
Swift Question

Translating code from Objective-C to Swift issues

In my app I want to add the following Objective-C code in Swift,

- (NSURL *)applicationDocumentsDirectory {
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask] lastObject];

My attempt has quite a few issues, aside from the syntax not being correct it seems that NSDocumentDirectory and NSUserDomainMask don't exist in Swift.

var applicationDocumentsDirectory : NSURL {

return NSFileManager.defaultManager().URLsForDirectory(NSDocumentDirectory, inDomains: NSUserDomainMask.lastObject)

The reason I'm using this computed property is that I need to pass an output URL to this camera framework I'm using as such...

// start recording
NSURL *outputURL = [[[self applicationDocumentsDirectory]
URLByAppendingPathComponent:@"test1"] URLByAppendingPathExtension:@"mov"];
[ startRecordingWithOutputUrl:outputURL];

Thank you for the help like always!

Answer Source

This is what you want:

NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).last
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download