I was wondering whether there are any other ways in swift to save data locally on device, which will be available also after app close without using NSUserDefaults.
If there are, in which cases is that specific solution preferable?
I know this is kind of general, but I know just this one way to do saving locally and from my experience, there is always more than one way to do something(unless there isn't).
Any answers would be greatly appreciated
Yes there are different ways to store data locally.
As you have mentioned one of them is
NSUserDefaults
In addition to that you can also:
Use files such as plist, JSON or your own custom format.
Use Core Data
Use SQLite
And also you can combine between those.
Here some nice post Core Data VS SQLite
Now for:
in which cases is that speciffic solution preferable
I would say that NSUserDefaults is good when you need to store some small pieces of data, for example: Numbers, Booleans, Strings.
Related
usually in Android I use </string-array name=""> tag in .xml file and refer to it inside the code by its name.
what is the best practice to create an array in IOS that I can use in multiple views without having to create it over and over again?
Depending on what values you want to store in your array and if you want to persist if you need to write into the array and persist it between app runs as well, you have several methods.
Store the array in UserDefaults. You can read from/write to UserDefaults from each of your ViewControllers and your data will be persisted even if the user quits the app.
Store it in a file. I wouldn't really recommend this option, unless you have a lot of data to store that can be easily represented as String/NSData.
Store data in a shared, singleton variable. This method is only suitable if you don't want to write into your array or you don't need to persist it between runs.
Use a database framework (CoreData or Realm). This is only recommended, if you have really complex data, but since you mentioned only an array, this shouldn't be your case.
Based on the amount of information provided in your question, I would suggest going for UserDefaults, it is quite easy to handle data in UserDefaults and it should be sufficient for your task.
I am currently creating an application that has a user, the user can follow others, post content, amongst a few other features and am wondering what would be the most ideal way to store and create data for a demo. I started building a rails site but felt that is becoming fairly time consuming for a demo application. Does anyone have any suggestions? pLists, JSON file, XML files?
What is the nature and size of the data?
pLists are convenient for a few dozen lines since they are easily converted into a dictionary at runtime.
If you have hundreds of items, you're probably better off writing an xml file and parsing it.
Simplest way is to way the object to FILE use Keyed Archiving. If you are bored about right almost same Keyed Archiving code each time. try to use Accessorizer
There is a possibility that a user of my app may enter tens/hundreds/thousands of pieces of information into the app. For example, they may enter names, addresses, phone numbers of various people.
What would you recommend is the best way to store (a potentially large amount) these strings?
Ideally, I'd like to be able to export the inputted data as a text delimited file or a spreadsheet.
You should consider either CoreData or sqlite. CoreData can be over an xml or sqlite database. If you use sqlite, you should consider the fmdb wrapper on github
But, a lot depends on the access patterns. Sqlite is very fast but it also offers querying capabilities.
Sqlite3 is a great way to store a large amount of data.
http://www.icodeblog.com/2008/08/19/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-1/
Failing that you could always use NSUserDefaults
I would simply like to create a file that stays around after the program quits. I would like it to contain objects that I have initiated, is that possible? Or does it just have to have text in it? How can I access that file later? Also, is this done the same way on the Mac and the iPhone?
Take a look at Core Data or SQLite. Apple has some sample code for them, not sure about SQLite because they prefer you tou use core data.
I will direct you to this question. Adding core data to my iPhone app
Or use NSUserDefaults: Best way to save data on the iPhone
Yes! It's possible.
Derive your objects that you want to persist from NSCoding. On those objects, implement the initWithCoder and encodeWithCoder functions on those objects. In these functions, write all of your primitive types to the coder (or read them) using the functions here http://developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/Classes/NSCoder_Class/Reference/NSCoder.html
When you want to save your file, use an NSKeyedArchiver and pass in the object you want to archive along w/ the file name.
[NSKeyedArchiver archiveRootObject:root toFile:string];
More examples here https://stackoverflow.com/questions/2805026/iphone-problem-in-saving-file-using-nscoding
I have used SQLitePersistentObjects in the past to give my Objective-C class objects an ORM-esque feel and behaviour. The objects itself translate into corresponding tables in the database, which in this case is a simple SQLite database file.
Archives and Serializations Programming Guide might be interesting as well.
In My application, how should i find meaning of the word, it should work both online and offline? Please help me out also is there any possiblilite to take local dictionary from iphone?
Sri
As far as I am currently aware, you aren't able to query the iPhone's built in dictionary.
Even if you could, I am not sure that it contains any definitions. Rather it looks for spelling correction on "close" words.
If you are wanting to replicate storing a dictionary inside your app, like the many dictionary apps currently out there, you would have to store the data in your iPhone app somehow.
Wikitionary provides free "dumps" of it's data which you could grab, and put into a form that would be usable in your app.
You would need to store the data in some form of plist, an SQLite database or CoreData.
For taking data from the internet, you would need to look at transferring the data using JSON, XML or PLIST.