I have a UitableView and its a checklist. I want to be able to save data when the user leaves the view. Then when the view is opened back up i want there to be the saved data. When i say saved data i mean that the table view is able to add and delete cells also i want to be able to save the checkmarks. Could somebody please provide me with a way or an idea on how to do this?
i know i can save data with:
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
and then save the data to defaults, but i need know how on saving the table view's cells that are add and or deleted! also i would like to know how to save the checkmarks!
Thank you,
Kurt
Sounds like core data would be good here
http://developer.apple.com/library/ios/#documentation/DataManagement/Devpedia-CoreData/coreDataOverview.html
I would suggest using either Core Data, or NSCoding. NSCoding allows you to encode an object as NSData, and reload a replica of that object from NSData.
For instance, saving and loading an array of strings through NSCoding would be something like this:
NSArray * array = [NSArray arrayWithObjects:#"This", #"Is", #"A", #"Test"];
NSData * encoded = [NSKeyedArchiver archivedDataWithRootObject:array];
// save the encoded data to a file...
// load the encoded data from a file...
NSArray * decodedArray = [NSKeyedUnarchiver unarchiveObjectWithData:encoded];
Of course, you will need to implement some NSCoding stuff yourself if you plan on using classes more complicated than NSDictionary, NSArray, NSString, etc.
Documentation for NSKeyedArchiver can be found here. You can also find documentation for the NSCoding protocol here.
Related
Throughout my iOS program. I create multiple arrays that all work together to make the program work. Now I need a method to store the arrays even when the iPhone is turned off. So I need these arrays to be stored like files that only I can touch.
I really would like to use User Defaults, but I just don't know if that is what I need. I have questions like:
How long will my data last in User Defaults?
When could you possibly lose the data?
If I was able to store arrays in User Defaults, how might I go about storing arrays in NSDictionaries? That way I could keep of my associated arrays together and give each array a specific key.
store array as NSData as in NSDefaults
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:urArray1] forKey:#"Array1"];
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:urArray2] forKey:#"Array2"];
and read it as
NSData *dataRepresentingSavedArray = [[NSUserDefaults standardUserDefaults] objectForKey:#"Array1"];
if (dataRepresentingSavedArray != nil)
NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
I think that what you want is to store the arrays on the disk and then (when you actually need them) get them back. You can start by seeing if the objects inside your NSMutableArrays conform with the NSCoding protocol. You can then simply write them to disk and load them when you need. For that you can use NSKeyedArchiver class.
As #The Saad pointed, and well, you can store the arrays using that piece of code. Although I wouldn't recommend it. You can check #roronoa zorro answer, as well.
NSUserDefaults basically just a wrapper around property list serialization.
NSUserDefaults provides a programmatic interface to Mac OS X's
preferences system. preferences are actually stored to the disk, they
are written to the user's Library/Preferences/ directory with the file
name equal to the bundle identifier with a plist extension.
Take a look at MikeBeam's article here
Well NSuSerDefaults is a way of storing persistent data in iPhone and you don't have to worry about losing of data or about it's persisitency as long as the application remains on the iPhone.
Yes, you can store the NSDictionary object in NSUserDefaults but it is advisable to use it for small Data.For bigger data you can use sqlite or any other DB.
Here's the Tutorial For NSUserDefault
I am new to this development.
I want to store complete object of a class into my database.
Actually I am creating application where user can add multiple views to parent view and want to save it, so that next time when user fetches it, he will get what ever he has saved i.e. views to parent view previously.
Any logic or suggestion on same will really be helpful,
Thanks in advance
Any object you want to save will need to conform to the NSCoder protocol. Keep in mind that if you have custom objects within your parent object that they to will need to conform to NSCoder. Add the following methods to your custom class(es).
- (id)initWithCoder:(NSCoder *)coder {
_inventory = [[coder decodeObjectForKey:#"inventory"] retain];
return self;
}
- (void)encodeWithCoder:(NSCoder *) encoder{
[encoder encodeObject:_inventory forKey:#"inventory"];
}
In the example above I want to encode a player's inventory. If the inventory contains custom objects (as opposed to a bunch of NSStrings for example) they'll also need their own NSCoder methods.
Below I turn it into something you can save out to NSUserDefaults. Adjust appropriately to store in a DB. Keep in mind if you want to send NSData over the wire to store in a DB you'll want to convert it to Base64 and possibly compress it.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData * encodedObject = [NSKeyedArchiver archivedDataWithRootObject:rootObject];
[defaults setObject:encodedObject forKey:kSaveArchiveKey];
[defaults synchronize];
To go the other way, you'll want to grab your NSData, do whatever magic on it as I described above (base64, compression) and unarchive.
PlayerInventory *inventory = [NSKeyedUnarchiver unarchiveObjectWithData:playerInventoryData]
You should choose between NSCoding and Core Data depending on your exact needs. See this post for more info: NSCoding VS Core data
You can store state(value of any attributes) of any object in database. You should use NSCoding.Example here
I have a Class for handling my data in my project, and now I need to store the data.
I'd like to use a Plist but I'm a bit unsure of how to start.
My class is pretty simple - 6 pieces of data, flat (no hierarchy).
I want my app to start with no data, so can I assume that I should create the PList programmatically once the User creates their first piece of data? (That is, don't create a .plist file in 'Supporting Files' prior to distribution?)
Then, when the app starts the next time, read the data and create an NSMUtableArray array of Class instances?
To create a property list, all you need to do is use appropriate types (i.e. those that support the property list format: NSData, NSString, NSDictionary, NSNumber, NSDate, NSArray), store them in a single container, and tell the containing object to write itself to a file. To read the data, you can initialize that same type using a path. For example:
// writing some data to a property list
NSString *somePath = ... // replace ... with the path where you want to store the plist file
NSMutableDictionary myDict = [NSMutableDictionary dictionary];
[myDict setObject:#"Caleb" forKey:#"name"];
[myDict setObject:[NSNumber numberWithInt:240] forKey:#"cholesterolOrIQ"];
[myDict writeToFile:somePath atomically:YES];
// reading the file again
NSDictionary *readDict = [NSDictionary dictionaryWithContentsOfFile:somePath];
The simplest way is to simple save an NSArray or NSDictionary to disk. Caleb's answer goes into detail there so I won't repeat it, other than to say you might have to convert a non-compatible object like NSColor to an property list object like NSData. It's up to you to do this each time you save or load your data.
NSKeyedArchiver and NSKeyedUnarchiver give you a little more control over the process, but work pretty much the same way. You provide (or get back) a plist compatible root object (usually an NSDictionary) that contains your data. I recommend creating a dictionary that includes your data structure as well as an arbitrary number (your app's build number is a good choice) to use as a version indicator. This way if you ever update your data model you can easily determine if you need to do anything to convert it to the new version.
If you're putting your own objects into the data file, look into NSCoding. The protocol gives you two methods using NSKeyedArchiver and NSKeyedUnarchiver to save and restore your data. This is by far the most straightforward approach if your data model consists of anything more than a few simple strings and numbers, since you're dealing with your own native objects. In your case, you would have your data class implement NSCoding and use the NSKeyedArchiver and NSKeyedUnarchiver methods to encode your six instance variables. When it's time to save or load, pack the instance of your class into an NSDictionary (along with a versioning number as I mentioned above) and call NSKeyedArchiver's archiveRootObject:toFile:. Your save an load methods deal only with your own data object, which makes things easy for you. The common pitfall to watch out for here is if your custom data object contains other custom object. This is fine, but you have to make sure every object that's going to be saved has its own NSCoding implementation.
Two things you can do:
Use NSUserDefaults:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html
The objectForKey method is the one you want to use to store your class. But, as pointed out in the comments, this shouldn't really be used for storing lots of user data; it's best for saving preferences.
For storing more data, you might want to look at Core Data. It's more complex, but should be better suited to your needs. Here's a tutorial on it:
http://mobile.tutsplus.com/tutorials/iphone/iphone-core-data/
Neither of these seems best for your simple application, but I leave this answer up since it gives alternatives for saving data to the iPhone.
As all who usually ask such questions I'm newbie in iPhone SDK programming and I've spend realy a lot of time to find the solution by my self.
So, I need to save a lot of app data of diff datatypes (bool,string,int,float,double,int arrays,double arrays and arrays of pointers) to file. There're many forms with fields, app settings etc. All that I need to save either user quit the app, or it was terminated unexpectedly. User could work with as much forms with fields, as it's possible. File is needed to open project with all filled forms and app sets on other device.
My trouble is that I couldn't find in what way I should wrilte all that data to file. I tried two diff ways. 1st: fill array with form's data, fill NSMutableArray with such arrays for every form. But for NSMutableArray there's a method (void)addObject:(id)anObject, and I didn't find how to get that (id)anObject from every array(or anything else) to add items.
The 2nd I've tried to use: NSMutableData. The same troubles: to fill with my data I need to convert them to Data ((void)appendData:(NSData *)otherData). In both cases objecs of that classes couldn't be filled correctly with my data. And I can't save it to file.
Maybe there's some better solution? Will be very apreciative for any help.
NSNumber is usually the way to go for basic stuff (integers, bools, and doubles/floats/etc). For example:
NSNumber *aBool = [NSNumber numberWithBool:YES];
NSNumber *aFloat = [NSNumber numberWithFloat:1.0f];
NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
[settings setValue:aBool forKey:#"some_key"];
The strings you can just add to NSUserDefaults because they are already objects (that conform to NSCoder, I believe), and for the NSArrays, just use the writeToFile: method:
NSArray *someArray = [NSArray arrayWithObjects:#"foo",#"bar",#"etc",nil];
[someArray writeToFile:#"filename" atomically:YES];
Some kind of serialization available in iPhone OS? Is that practically possible or should I quickly forget about that?
I am making a tiny app that stores some inputs in an NSMutableArray. When the user leaves, the inputs should stay alive until he/she returns to continue adding or removing stuff to/from that array.
When the app quits, there must be some way to store all the stuff in the array in a file. Or must I iterate over it, rip everything out and write it i.e. comma-separated somewhere, then the next time go in again, read the stuff in, and iterate over the lines in the file to make an array with that data? That would be hard like a brick. How to?
The easy way, since you already have an NSArray object is to write the array to disk using
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag
and read it back in with:
- (id)initWithContentsOfFile:(NSString *)aPath
or
+ (id)arrayWithContentsOfFile:(NSString *)aPath
You can also use NSCoder.
You can probably search sof for the right code.
So long as the objects in the array implement NSCoding (NSString and NSValue do; if you're storing your own objects it's relatively straightforward), you can use:
[NSKeyedArchiver archiveRootObject: array toFile: filePath];
to save and:
array = [NSKeyedUnarchiver unarchiveObjectWithFile: filePath];
to load.
You can similarly load/save to NSData.
The iPhone SDK is designed to store data using SQLite tables.
You can use NSPropertyListSeralization, since NSArray is a type of property list. You can find more information here.