Swift – Unable to read/write from file
I’m actually not sure whether I’m not able to read or unable to write. I’m pretty sure writing works. I’m using Swift.
Here are my project files.
I’m trying to write an array of arrays to a file, and then read that array of arrays when the app launches. Here’s my code for writing and reading, respectively.
listOfTasks is my array of arrays variable.
let cocoaArray : NSArray = listOfTasks cocoaArray.writeToFile(String(fileURL), atomically: true)
listOfTasks = NSArray(contentsOfFile: String(fileURL)) as! [Array<String>]
let documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).last let fileURL = documentsDirectory!.URLByAppendingPathComponent("file.txt")
I’m happy to provide additional information as necessary. Thanks in advance!
- NSURLConnection download large file (>40MB)
- Saving a recorded AVAudioRecorder sound file: Now what? ( iOS, Xcode 4 )
- iOS: How to read an audio file into a float buffer
- Objective C: Write a file programmatically to the Main Bundle
- Writing and reading custom object to file IOS
- iOS : I can't write my file
Solutions Collect From Internet About “Swift – Unable to read/write from file”
Not sure where’s exactly your problem, but here’s a full working Playground sample:
let fileUrl = NSURL(fileURLWithPath: "/tmp/foo.plist") // Your path here let array = [["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"], ["One", "Two", "Three"]] // Your array of arrays here // Save to file (array as NSArray).writeToURL(fileUrl, atomically: true) // Read from file let savedArray = NSArray(contentsOfURL: fileUrl) as! [[String]] print(savedArray)
- How should beginbackgroundtaskwithexpirationhandler: be dealt with for an NSUrlConnection that is already in progress?
- How to “Show my current location on google maps, when I open the ViewController?” in Swift?
- Failed to locate or generate matching singing assets Xcode 7.2.1
- Wrong value returned after formatting timestamp
- How to parse a string of hex into ascii equivalent in Swift 2
- Remove white space from contact number fetched from phone book
- HTTP digest authentication fail due to wrong nonce-count in iOS 10
- iPhone6 Display Zoom Functionality disturbs the whole view
- Adding items to Swift array across multiple threads causing issues (because arrays aren't thread safe) – how do I get around that?
- iOS – 2d image turn into a 3d
- How set Custom Annotation markers ( animated rings around a point) on GMSMapView
- UITableViewController inside UIScrollView with Horizontal Paging
- Can I have a multipeer connectivity session run in the background?
- When converting a project to use ARC what does “switch case is in protected scope” mean?
- IOS Safari support plugins?