var = str creates 'ambiguous use of appendingPathComponent' error - swift

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")

Related

How to load data from Local storage... I tried many things but nothing works

Ho to show the image from below url
URL->
file:///Users//Library/Developer/CoreSimulator/Devices/F4A0B7EA-292B-4DFC-B6A0-8AE0DD80BBD1/data/Media/DCIM/100APPLE/IMG_0005.JPG
Use this to get url-
var paths: [Any] = NSSearchPathForDirectoriesInDomains(.documentDirectory,
.userDomainMask, true)
let documentsDir: String? = (paths[0] as? String)
let url: String = documentsDir! + "/IMG_0005.JPG"

Partial component from URL in Swift

I have this URL:
file:///var/mobile/Containers/Data/Application/182BB05D-E460-42CD-9030-EBD907799E0F/Documents/video/f9cf9f593a3f50c9ee50acb51aaeee1ee1febbe3c2cd4aded49b39e01b42f9ac/6B617D7C-F153-4F7C-BE02-4362CAC4A4C9.mov
And I want to get just this string back from the URL
video/f9cf9f593a3f50c9ee50acb51aaeee1ee1febbe3c2cd4aded49b39e01b42f9ac/6B617D7C-F153-4F7C-BE02-4362CAC4A4C9.mov
Is using pathComponents the only way?
You can try to get the documents directory path and remove it from your video file path.
Example :
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let videoPath = "file:///var/mobile/Containers/Data/Application/182BB05D-E460-42CD-9030-EBD907799E0F/Documents/video/f9cf9f593a3f50c9ee50acb51aaeee1ee1febbe3c2cd4aded49b39e01b42f9ac/6B617D7C-F153-4F7C-BE02-4362CAC4A4C9.mov"
videoPath.replacingOccurrences(of: documentsPath, with: "")
Try this..
var filePath = "file:///var/mobile/Containers/Data/Application/182BB05D-E460-42CD-9030-EBD907799E0F/Documents/video/f9cf9f593a3f50c9ee50acb51aaeee1ee1febbe3c2cd4aded49b39e01b42f9ac/6B617D7C-F153-4F7C-BE02-4362CAC4A4C9.mov"
if let range = filePath.range(of: "video")?.lowerBound {
let url = filePath.substring(from: range)
}

Ambiguous use of variable in Swift

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

How to get user home directory path (Users/"user name") without knowing the username in Swift3

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.

instance member "baseURL" cannot be used on type "URL"

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
}