Tag Archives: File

Read and Write Array, Dictionary and Other Collections to Files

http://iosdevelopertips.com/data-file-management/read-and-write-array-dictionary-and-other-collections-to-files.html

 

With just a few lines of code, you can read/write collections to/from files. The code below shows examples for writing and reading back both arrays and dictionaries.

Read and Write Collections to File

 

Write/Read data to .plist file

http://ipgames.wordpress.com/tutorials/writeread-data-to-plist-file/

First of all add a plist to your project in Xcode. For example “data.plist”.
Next, look at this code which creates path to plist in documents directory:

1) Create a list of paths.
2) Get a path to your documents directory from the list.
3) Create a full file path.
4) Check if file exists.
5) Get a path to your plist created before in bundle directory (by Xcode).
6) Copy this plist to your documents directory.

Read data:

 

Write data:

 

Remember about two things:


1) You must create a plist file in your Xcode project.

2) To optimize your app, better is to save all the data when application (or for example view) is closing. For instance in applicationWillTerminate. But if you are storing really big data, sometimes it cannot be saved in this method, because the app takes too long to close and the system will terminate it immediately.