Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Dewan Payroda
1,954 PointsDownload file path with NSFileManager Swift
Hi I have a whole bunch of .mp3 files I want to use with NSFileManager and store in the documents folder. Is there a way I can download the .mp3 files online and then have it save to the documents folder? This is what I got so far but no luck. I have multiple URL's so if you recommend I put them in an array please explain how I should exactly do that. Really lost here.
override func viewDidLoad() { self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
let filemanager = NSFileManager.defaultManager()
let documentsPath : AnyObject = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)[0]
let destinationPath:NSString = documentsPath.stringByAppendingString("/Untitled.mp3")
if (!filemanager.fileExistsAtPath(destinationPath)) {
var theError: NSError?
let url = "https://www.box.com/shared/static/4k43665bm1d65lqid55v.mp3"
let data = NSData(contentsOfFile: url, options: nil, error: nil)
if (data != nil) {
let writePath = destinationPath.stringByAppendingPathComponent(url)
filemanager.copyItemAtPath(writePath,toPath:destinationPath, error: &theError)
if (theError == nil) {
println("The music files has been saved.")
} else {
println("Error")
}
}
} else {
println("The files already exist")
}
}