ios - Swift - Unable to read/write from file -
i'm not sure whether i'm not able read or unable write. i'm pretty sure writing works. i'm using swift.
here project files.
i'm trying write array of arrays file, , read array of arrays when app launches. here's code writing , reading, respectively. listoftasks
array of arrays variable.
writing:
let cocoaarray : nsarray = listoftasks cocoaarray.writetofile(string(fileurl), atomically: true)
reading:
listoftasks = nsarray(contentsoffile: string(fileurl)) as! [array<string>]
fileurl
:
let documentsdirectory = nsfilemanager.defaultmanager().urlsfordirectory(.documentdirectory, indomains: .userdomainmask).last let fileurl = documentsdirectory!.urlbyappendingpathcomponent("file.txt")
i'm happy provide additional information necessary. in advance!
not sure where's problem, here's full working playground sample:
let fileurl = nsurl(fileurlwithpath: "/tmp/foo.plist") // path here let array = [["mon", "tue", "wed", "thu", "fri", "sat", "sun"], ["one", "two", "three"]] // array of arrays here // save file (array nsarray).writetourl(fileurl, atomically: true) // read file let savedarray = nsarray(contentsofurl: fileurl) as! [[string]] print(savedarray)