Saving data to plist per object or person - ios

I am trying to save data to the plist in my app per person, but it save it to everyone, so for example if I have a field for eye color and I enter Brown in the textfield for John Doe, it save Brown for everyone else as well, is there a way to save this info per person in the app instead using plist? I have had no luck trying:
-(NSString *)pathofFile{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsfolder = [paths objectAtIndex:0];
return [docsfolder stringByAppendingFormat:#"data.plist"];
}
Here is the code I have in my view did load:
- (void)viewDidLoad {
[self loadPerson];
[super viewDidLoad];
NSString *filepath = [self pathofFile];
if ([[NSFileManager defaultManager]fileExistsAtPath:filepath]) {
NSArray *array = [[NSArray alloc]initWithContentsOfFile:filepath];
Drinfo.text = [array objectAtIndex:0];
[array release];
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(applicationWillTerminate:) name: UIApplicationWillTerminateNotification object:app];
and here is what I have as part of my saveperson method:
NSMutableArray *array = [[NSMutableArray alloc]init];
[array addObject:self.Drinfo.text];
[array writeToFile:[self pathofFile] atomically:YES];
[array release];

You could use a plist just create a NSDictionary for each person inside the plist and add the colour string to that. The steps would be something like
Open up the plist from file, set it to be a NSDictionary and make sure its a mutable copy.
Create a new NSMutableDictionary and add any attributes to that e.g. eye colour.
Add that mutable dictionary to your plist dictionary with the key set to the persons name
Save the plist

Related

removeObjectForKey doesn't remove value from .plist

I am developing iPhone app in which i have TableView,
What i want to do is On click of UITableViewCell i want to remove this values from plist, MOBILES.Brand.2 and MOBILES.Brand.5 if they exist in .plist if they doesn't exist in .plist then i want to add it to my .plist
Here is the structure of my .plist:
:
Here is my code snippet:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
plistPath = [self getPlistPath];
//PlistDict is mutableDictionary contains the keys and values of .plist (of above image)
if ([PlistDict valueForKeyPath:#"MOBILES.Brand.2"] != nil) { //if key and it's value exists in Filter.plist
//Delete Key from .plist...
NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
[savedStock removeObjectForKey:#"MOBILES.Brand.2"];
[savedStock removeObjectForKey:#"MOBILES.Brand.5"];
[savedStock writeToFile:plistPath atomically:YES];
}else{
//Add Key to .plist
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
[data setValue:#"251" forKeyPath:#"MOBILES.Brand.2"];
[data setValue:#"298" forKeyPath:#"MOBILES.Brand.5"];
[data writeToFile:plistPath atomically:YES];
}
}
-(NSString*)getPlistPath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"Filter.plist"];
PlistDict = [NSMutableDictionary dictionaryWithContentsOfFile:path];
return path;
}
after writing above code snippet it doesn't remove the keys from .plist but it adds the key successfully if not existed.
Where i am doing mistake ? please help and thanks in advance.
The problem is you are trying to remove using the NSMutableDictionary keypath directly using removeObjectForKey that requires the exact key not keypath and you are supplying a keypath. Use the following NSMutableDictionary Category taken from here
#interface NSMutableDictionary (Additions)
- (void)removeObjectForKeyPath: (NSString *)keyPath;
#end
#implementation NSMutableDictionary (Additions)
- (void)removeObjectForKeyPath: (NSString *)keyPath
{
// Separate the key path
NSArray * keyPathElements = [keyPath componentsSeparatedByString:#"."];
// Drop the last element and rejoin the path
NSUInteger numElements = [keyPathElements count];
NSString * keyPathHead = [[keyPathElements subarrayWithRange:(NSRange){0, numElements - 1}] componentsJoinedByString:#"."];
// Get the mutable dictionary represented by the path minus that last element
NSMutableDictionary * tailContainer = [self valueForKeyPath:keyPathHead];
// Remove the object represented by the last element
[tailContainer removeObjectForKey:[keyPathElements lastObject]];
}
#end
It should work for you.
Method - 2
If the above doesn't work, you can do an iteration of your dictionary, and specifically delete the object for the key.. Try
NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
NSMutableDictionary *brand=[[savedStock objectForKey:#"MOBILES"] objectForKey:#"Brand"];
[brand removeObjectForKey:#"2"];
[brand removeObjectForKey:#"5"];
Cheers.

Append New Input to plist in Documents Folder

After much searching of SO I've been able to come up with code to correct previous failures. What I am trying to accomplish is accepting user input from a UITextField and add that input to my TableView, which is populated from a plist. The root value of my plist is dictionary and I would like to keep it as such. My problem is when writeToFile is called, I'm completely over writing the existing values instead of inserting, adding or appending to the existing data. It doesn't appear that my attempt to combine the two dictionaries is working, since only the new value is being stored to the plist. Any insight as to where I'm going wrong? The following code is what I have
//////////********** Add New Cell When OK is Chosen
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
UITextField *newDevice = [alertView textFieldAtIndex:0];
NSLog(#"newDevice - %#",newDevice.text);
if (alertView.tag == 1 && buttonIndex == 1){
NSMutableDictionary *input = [[NSMutableDictionary alloc]init];
[input setObject:[NSArray arrayWithObject:newDevice.text] forKey:#"Room"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"Test.plist"];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
// Combine the two Dictionaries to create one
[dictionary addEntriesFromDictionary:input];
// Write Combined Dictionary to plist
[dictionary writeToFile:path atomically:YES];
// Add Newly Created text to Table because reload table doesn't do it
[myTableData addObject:newDevice.text];
// Reload Table Data even though it seems useless
[myTable reloadData];
}
}
I guess you overwrote the values of the key Room.
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"Test.plist"];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
// Get exist input from plist
NSMutableArray *inputFromPlist = dictionary[#"Room"];
// add new object to input
[inputFromPlist addObject:newDevice.text];
// set Room with modified object
[dictionary setObject:inputFromPlist forKey:#"Room"];
// Write Combined Dictionary to plist
[dictionary writeToFile:path atomically:YES];
// do something after saving plist ...

Plist dicionaries keys

I am using this code to add content into a plist :
//////////// Save data into plist /////////////////
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"datatesting.plist"];
NSLog(#"path='%#'",path);
NSFileManager *nfm = [[NSFileManager alloc] init];
if([nfm fileExistsAtPath:path])
{
// if file exists, get its contents, add more entries and write back
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSArray *keys = [NSArray arrayWithObjects:#"Title",#"Description",#"Coordinate",nil];
[array addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSString stringWithFormat:#"%#",titlestring],[NSString stringWithFormat:#"%#",descriptionstring],[NSString stringWithFormat:#"%#",coordinadastring], nil] forKeys:keys]];
NSLog(#"modified array=%#",array);
BOOL ok = [array writeToFile:path atomically:YES];
if(!ok){
NSLog(#"Unable to write appended file");
return;
}
} else {
// if file doesn't exist, create a new one
NSMutableArray *array = [[NSMutableArray alloc] init];
NSArray *keys = [NSArray arrayWithObjects:#"Title",#"Description",#"Coordinate",nil];
[array addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSString stringWithFormat:#"%#",titlestring],[NSString stringWithFormat:#"%#",descriptionstring],[NSString stringWithFormat:#"%#",coordinadastring], nil] forKeys:keys]];
NSLog(#"new array=%#",array);
BOOL ok = [array writeToFile:path atomically:YES];
if(!ok){
NSLog(#"Unable to write new file");
return;
}
}
Now I am having issues in using the content of the plist. So my two questions are :
- What is the Keys of the dictionaries of the current plist ?
- What is the way to read the content in a Tableview ?
You have an array of dictionaries. You need to iterate on the contents of the array, then look at each dictionary. You can do this with block enumerators or the older for() style. I'll use the latter now as you may not know blocks.
NSArray *array = ...; // read in the array from the file
for(NSDictionary *dict in array) {
NSString *title = [dict objectForKey:#"Title"];
... etc
}
When you want to show this data in a tablview, the number of rows is the number of dictionaries (ie [array count]). If you wanted to show just the title for each cell, you would get the cell row number, then get the dictionary by [array objectAtIndex:cellRow]), then pull out the title as above.

Load data from plist to tableview [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to populate UITableView with plist
I save data to plist like this:
- (IBAction)save:(id)sender
{
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"data.plist"];
NSMutableArray *data = [NSMutableArray arrayWithContentsOfFile:plistPath];
if (nil == data) {
data = [[NSMutableArray alloc] initWithCapacity:0];
}
else {
[data retain];
}
NSMutableDictionary *array = [[NSMutableDictionary alloc]init];
[array setObject:label1.text forKey:#"Test1"];
[array setObject:label2.text forKey:#"Test2"];
[array setObject:label3.text forKey:#"Test3"];
[array setObject:label4.text forKey:#"Test4"];
NSLog(#"%#", array);
[data addObject:array];
[array release];
[data writeToFile:plistPath atomically: TRUE];
[data release];
}
NSLog:
2012-07-01 18:52:19.566 testapp[22651:707] {
Test1 = 40;
Test2 = 102;
Test3 = 153;
Test4 = 255;
}
How can I load saved data to uitableview?
To read it, do this:
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
You should then be able to read the values for all the keys like this:
NSString *myValue = [plistDict objectForKey:#"someKey"];
edit:
After rereading your code it seems like you might be doing something you don't want to do. Your current plist structure is a top level array that adds a dictionary to itself every time you invoke "save". Is this what you're trying to do? If so, what represents a cell in your table, an index in the array?
If so, then you want to do
// top level array
NSMutableDictionary* plistArray = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
// individual dictionary at one array index (such as indexPath.row in cellForRowAtIndexPath)
NSMutableDictionary *dictionary = [plistArray objectAtIndex:yourIndex];
In any case, the object you have named "array" is actually a dictionary so you should rename it (or change it to be an array if that's what you want).

Saving Data in iOS5 Issue

I'm trying to save simple text data in my app using this code from Beginning iPhone Development:
- (NSString *)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingFormat:kFileName];
}
- (void)applicationWillTerminate:(NSNotification *)notification {
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:field1.text];
[array addObject:field2.text];
[array addObject:field3.text];
[array addObject:field4.text];
[array writeToFile:[self dataFilePath] atomically:YES];
}
- (void)viewDidLoad { NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
field1.text = [array objectAtIndex:0];
field2.text = [array objectAtIndex:1];
field3.text = [array objectAtIndex:2];
field4.text = [array objectAtIndex:4];
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationWillTerminate:)
name:UIApplicationWillTerminateNotification
object:app];
[super viewDidLoad];
}
This code was written and works great for iOS 3, but not with iOS 5. If you simply tap the home button in the simulator to close it, then bring the multitasking bar up and tap on the app icon, the data reloads just fine. But, when you close it via tapping the home button, then bring up the multitasking bar and close it there as well, and relaunch the app, the data is gone. I don't think the app is getting the notification that it is being terminated when you close it from the multitasking bar, but I'm not sure about that I'm still a beginner.
I'm sure that I just need to add an extra line or 2 to the code that I already have. Does anyone know what that might be?
May I suggest using the NSUserDefaults class? It's very good for storing simple data like this.
Setting a value
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:#"value" forKey:#"field"];
[defaults synchronize];
Getting a value
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
NSString * value = [defaults objectForKey:#"field"]
My guess is that when this was written for iOS 3, applicationWillTerminate was called when you hit the home button. With iOS 5, this isn't the case. Try moving your code to -applicationDidEnterBackground.

Resources