I converted my Swift to the current Swift syntax and now get the following error in this code:
let paths = NSSearchPathForDirectoriesInDomains(
FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let documentsDirectory: AnyObject = paths[0] as AnyObject
let dataPath = documentsDirectory.appendingPathComponent(saveFileName)
Error: Ambiguous use of appendingPathComponent
Basically
never cast a type up to something more unspecific.
never cast a distinct known type at all.
NSSearchPathForDirectoriesInDomains returns clearly [String], so delete the annotation and the cast.
let documentsDirectory = paths[0]
However it's recommended to use the URL related API because in Swift 3 the path functions in String have been removed.
let documentsDirectory = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let dataURL = documentsDirectory.appendingPathComponent(saveFileName)
If you really need the string path add
let dataPath = dataURL.path
Related
I have been racking my brain on how to create and write a plist to a certain Folder Directory in MacOS. In my case to the LaunchDaemons folder in /Library. I know how to create the plist but its the writing to the LaunchDaemons folder that I am having issues with. This code below from my understanding is for the sandbox but how do I do it outside of the sandbox? Cheers
let fileManager = FileManager.default
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let path = documentDirectory.appending("test.plist")
I have added the code with the help I have received and have no errors but it is not writing anything to the folder. Here is the code:
let libraryDirectory = try! FileManager.default.url(for: .libraryDirectory, in: .localDomainMask, appropriateFor: nil, create: false)
let launchDaemonsFolder = libraryDirectory.appendingPathComponent("LaunchDaemons/test.plist")
if FileManager.default.fileExists(atPath: launchDaemonsFolder.path) {
print(launchDaemonsFolder)
let plistDictionary : [String: Any] = [
"ExitTimeOut": 600,
"Label": "BOOT.SHUTDOWN.SERVICE",
"ProgramArguments": ["/test.sh"] as Array,
"RunAtLoad": false,
"WorkingDirectory": "/"
]
let dictionaryResult = NSDictionary(dictionary: plistDictionary)
let fileWritten = dictionaryResult.write(to: launchDaemonsFolder, atomically: true)
print("is the file created: \(fileWritten)")
} else {
print("File Exists")
}
First of all NSSearchPathForDirectoriesInDomains is outdated, it's highly recommended to use the URL related API anyway.
This code creates an URL pointing to /Library/LaunchDaemons
let libraryDirectory = try! FileManager.default.url(for: .libraryDirectory, in: .localDomainMask, appropriateFor: nil, create: false)
let launchDaemonsFolder = libraryDirectory.appendingPathComponent("LaunchDaemons")
This is the method I'm trying to fire:
mMovieFileOutput?.startRecording(toOutputFileURL: URL(mov_path), recordingDelegate: self)
I'm getting this error:
Argument labels '(_:)' do not match any available overloads
Dose anyone know what arguments I need?
You need to use one of the valid initializers for URL.
Here is some basic Swift 3 code to demonstrate this.
var videoFileOutput = AVCaptureMovieFileOutput()
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let filename = NSUUID().uuidString + ".mov"
let filePath = documentsURL.appendingPathComponent(filename)
videoFileOutput.startRecording(toOutputFileURL: filePath, recordingDelegate: self)
If you want to use some other URL initializer for some reason, just use a valid one. As #Anton said, URL(fileURLWithPath: "") is correct initializer for a path.
Cheers
I am working on a decibel measuring app for my class, and I have been stumped by an error that keeps on coming up: 'ambiguous use of appendingPathComponent'. Here is where the problem is occurring:
//set up the URL for the audio file
var documents: AnyObject = NSSearchPathForDirectoriesInDomains( FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0] as AnyObject
var str = documents.appendingPathComponent("recordTest.caf")
var url = NSURL.fileURL(withPath: str as String)
The error is happening here:
var str = documents.appendingPathComponent("recordTest.caf")
I can't seem to get this resolved.
Help,
Paul
Why are you casting documents to AnyObject? Get rid of that.
But that then brings up a new issue since appendingPathComponent is a method of NSString, NSURL, or URL. But documents is a String.
And why use NSURL instead of URL?
Since your goal is to get a URL, use the more direct approach with FileManager:
let docURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let url = docURL.appendingPathComponent("recordTest.caf")
I have this code:
let image = UIImage(data: downloadedImage!)
let convertImage = UIImagePNGRepresentation(image!)
let pathUIDImage = self.getDocumentsDirectory().appendingPathComponent(playersUID + "Image")
try? convertImage!.write(to: pathUIDImage) //working
let playersUIDImageVersion = "1"
var pathUIDImageVersion = self.getDocumentsDirectory().appendingPathComponent(playersUID + "ImageVersion")
try? playersUIDImageVersion.write(to: pathUIDImageVersion) //error
The error is:
Cannot convert value of type "URL" to expected instrument type "inout_"
When I replace try? playersUIDImageVersion to try? convertImage! the error is gone. Is there a difference between writing different types of values to the directory? Thank you. Below the function getDocumentsDirectory
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentsDirectory = paths[0]
return documentsDirectory
}
The write function of String is different, it requires additional parameters atomically and encoding.
try? playersUIDImageVersion.write(to: pathUIDImageVersion, atomically: true, encoding: .utf8)
In such a case retype the function and see what Xcode suggests for code completion.
Besides it's highly recommended to use always an appropriate file extension (.png, .txt).
On the fifth line i m getting the error that " instance member "baseURL" cannot be used on type "URL". Someone please help me solve this error.
func getFileURL() -> URL {
let fileName = (imagePath! as NSString).lastPathComponent
let dirPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let pathArray:[String] = [dirPath, fileName]
let fileURL = URL.baseURL(withPathComponents: pathArray)
return fileURL!
}
let fileURL = URL.baseURL(withPathComponents: pathArray)
You're calling baseURL on the class URL, not an actual instance of URL. It would be like trying to eat not an apple, but the word "apple". Try this instead:
func getFileURL() -> URL {
let fileName = (imagePath! as NSString).lastPathComponent
let dirPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let fileURL = URL(fileURLWithPath: dirPath, isDirectory: true).appendingPathComponent(fileName)
return fileURL.baseURL!
}
EDIT: I'm not exactly sure about what you're trying to ask, so you may need to change return fileURL.baseURL! to return fileURL.
func getFileURL() -> String {
var paths: [AnyObject] = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
filePath = paths[0].stringByAppendingString("/output/outPut.mov") // whatever filetype you want
return File Path
}