I am developing an application where the user creates an event which has 3 fields:
Category , name , event. After the user gives his entry , i have a save button that will save his data for future reference. Then when he opens the app again the data will be shown in a table View.
How exactly do i "save" data on iOS ? I know about the NSUserDefaults , but i am pretty sure this is not the way for this example.
What i ve done so far :
I created a "Note" class with Category , name , event.
The code for my save button looks like this:
- (IBAction)save:(id)sender {
//creating a new "note" object
Note *newNote = [[Note alloc]init];
newNote.category = categoryField.text;
newNote.name = nameField.text;
newNote.event = eventField.text;
// do whatever you do to fill the object with data
NSData* data = [NSKeyedArchiver archivedDataWithRootObject:newNote];
/*
Now we create the path to the documents directory for your app
*/
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
/*
Here we append a unique filename for this object, in this case, 'Note'
*/
NSString* filePath = [documentsDirectory stringByAppendingString:@"Note"];
/*
Finally, let's write the data to our file
*/
[data writeToFile:filePath atomically:YES];
/*
We're done!
*/
}
Is this the correct way to save an event? How can i retrieve now what i wrote ?
Secondly if i run this code again i ll overwrite data , or create new entry?
I would like to see how i can make a new entry every time.
Also i would like to delete an event from the table that i am presenting them , so i would like to see how the delete would work.
My "Note" object looks like that:
@interface Note : NSObject <NSCoding> {
NSString *category;
NSString *name;
NSString *event;
}
@property (nonatomic, copy) NSString *category;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *event;
@end
You can use core data to save all your data and delete it when you want. The code above always create new object of Note class so every time new data you have but when you will try to write with the same name "Note". It always overwrite the old data.
All are correct besides that you can use Sqlite to save the data locally with your application.
This is just a file but accepts all standard sql statements.
This is also one way to save the data locally..
Try
//Note.m file
Save a note by
Source Code
You can use NSKeyedUnArchiver to retrieve the data. It will over write if you try to write in the same file path
Look, I am giving you the General Idea, you can use this code according to your requirement.
1) Get the "Path" of
yourPlist.plist
file :2) Insert data into the yourPlist :
3) Retrieve data from the yourPlist :
For saving data via NSUserDefaults I'm using GVUserDefaults
Usage
Create a category on GVUserDefaults, add some properties in the .h file and make them @dynamic in the .m file.
Now, instead of using
[[NSUserDefaults standardUserDefaults] objectForKey:@"userName"]
, you can simply use[GVUserDefaults standardUserDefaults].userName
.You can even save defaults by setting the property: