I have a file on my app I've stored dynamically using NSFileManager. when I loop through all the files I can see the full path to the file:
let fileManager:NSFileManager = NSFileManager.defaultManager()
for(var i:Int = 0; i < count; i++){
if fileManager.fileExistsAtPath(fileList[i]) != true {
print("File path: \(fileList[i])")
}
}
I get the correct path, in this case: /var/mobile/Containers/Data/Application/123321ABF/Documents/myFile.zip
All good until here. But then I'd like to upload it to an FTP server and for that I need to turn the path into NSURL but this doesn't seem to work, like it can't find the file, I've tried:
let url = NSURL(string: fileList[i])
and
let url = NSURL(fileURLWithPath: fileList[i], isDirectory: false)
but I keep getting the same error:
Error Domain=NSCocoaErrorDomain Code=2 "(null)"
Try the following code:
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let files = NSFileManager.defaultManager().enumeratorAtURL(documentsURL, includingPropertiesForKeys: nil, options: [], errorHandler: nil)
while let file = files?.nextObject() {
print("***")
let modFile = file as! NSURL
print("FILE: \(modFile)")
print("***")
}
If I understood you wrong, please feel free to correct me! Regards, Alex
Simple answer is below
let URL: NSURL = NSURL(string: stringofURL)! //replace stringofURL to Path
Example
// just a string
var stringUrl = "/var/mobile/Containers/Data/Application/123321ABF/Documents/myFile.zip"
// convert path to NSURL
let URL: NSURL = NSURL(string: stringUrl)!
print (URL) // print url
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.