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
Related
I'm updating my app with the new iOS 11 standards and a lot of stuff were deprecated and now I'm stacked with this error: "Argument labels '(contentsOfFile:)' do not match any available overloads.
Here you are the codes that was working:
//load plist file
var palermoip: NSArray?
if let path = Bundle.main.path(forResource: "palermoip", ofType: "plist") {
palermoip = NSArray(contentsOfFile: path)
}
Anyone knows how can I fix it? Thank you in advance !
I recommend to use PropertyListSerialization and the URL related API
let url = Bundle.main.url(forResource: "palermoip", withExtension: "plist")!
let data = try! Data(contentsOf:url)
let palermoip = try! PropertyListSerialization.propertyList(from: data, format: nil) as! [[String:Any]] // or [Any] if the array does not contain dictionaries
and in Swift 4 even PropertyListDecoder
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).
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
I am making a func that edits a text file in the Users/johnDoe Dir.
let filename = "random.txt"
let filePath = "/Users/johnDoe"
let replacementText = "random bits of text"
do {
try replacementText.write(toFile: filePath, atomically: true, encoding: .utf8)
}catch let error as NSError {
print(error: + error.localizedDescription)
}
But I want to be able to have the path universal. Something like
let fileManager = FileManager.default
let downloadsURL = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first! as NSURL
let downloadsPath = downloadsURL.path
but for the JohnDoe folder. I haven't been able to find any documentation on how to do this. The closest thing I could find mentioned using NSHomeDirectory(). And I am not sure how to use it in this context.
when I try adding it like...
let fileManager = FileManager.default
let downloadsURL = FileManager.default.urls(for: NSHomeDirectory, in: .userDomainMask).first! as NSURL
let downloadsPath = downloadsURL.path
I get an error:
"Cannot Convert value of type 'String' to expected argument type 'FileManager.SearchPathDirectory'"
I've tried it .NSHomeDirectory, .NSHomeDirectory(), NShomeDirectory, NShomeDirectory()
You can use FileManager property homeDirectoryForCurrentUser
let homeDirURL = FileManager.default.homeDirectoryForCurrentUser
If you need it to work with earlier OS versions than 10.12 you can use
let homeDirURL = URL(fileURLWithPath: NSHomeDirectory())
print(homeDirURL.path)
There should be an easier way but -- at worst -- this should work:
let filePath = NSString(string: "~").expandingTildeInPath
Swift 5 (and maybe lower)
let directoryString: String = NSHomeDirectory()
let directoryURL: URL = FileManager.default.homeDirectoryForCurrentUser
Maybe
FileManager.homeDirectoryForCurrentUser: URL
It's listed as "beta" for 10.12, though.