NSMutableArray Problem - iPhone - iphone

I'm trying to get a UITableView to read it's data from a file. I've attempted it like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullFileName = [NSString stringWithFormat:#"%#/entries.plist", documentsDirectory];
self.dataForTable = [[NSMutableArray alloc] initWithContentsOfFile:fullFileName];
This compiles fine, but when saving something to the file in the following snippet, the file is not saved nor anything is written to the array:
NSMutableDictionary*userDictionary;
userDictionary = [[NSMutableDictionary alloc] init];
[userDictionary setObject:name.text forKey:#"name"];
[userDictionary setObject:email.text forKey:#"email"];
[userDictionary setObject:serial.text forKey:#"serial"];
[userDictionary setObject:notes.text forKey:#"notes"];
[userDictionary setObject:[NSNumber numberWithInt:[licenseType selectedRowInComponent:0]] forKey:#"license_type"];
[userDictionary setObject:[date date] forKey:#"date"];
[userDictionary setObject:[NSNumber numberWithBool:[paymentSwitch isOn]] forKey:#"payment"];
NSString*dirToSaveTo = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString*fileName = [NSString stringWithFormat:#"%#.plist",name.text];
NSString*saveName = [dirToSaveTo stringByAppendingPathComponent:fileName];
[userDictionary writeToFile:saveName atomically:NO];
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullFileName = [NSString stringWithFormat:#"%#/entries.plist", documentsDirectory];
[self.dataForTable addObject:name.text];
NSLog(#"%#",self.dataForTable);
[self.dataForTable writeToFile:fullFileName atomically:YES];
The NSLog just returns (null). The *plist file is never written. What am I doing wrong?

Almost certainly this line is failing, and you're not checking whether it returned nil:
self.dataForTable = [[NSMutableArray alloc] initWithContentsOfFile:fullFileName];
Once it does return nil, every other call to it does nothing. The most likely problems are that the file path you have constructed is incorrect and doesn't point to the file, or that the file is not a proper plist.

Related

Why doesn't my NSMutableArray write to plist?

I have the following block of code:
NSMutableArray* mergedSymbolsArray = [NSMutableArray array];
for (NSDictionary* aSymbol in localSet) {
NSLog(#"Symbol:%#",[aSymbol valueForKey:#"symbol"]);
[mergedSymbolsArray addObject:aSymbol];
}
[Utils writeObjectToPList:mergedSymbolsArray];
tickers = [Utils getDataFromPList];
Here is my code to read/write to a plist:
+ (void)writeObjectToPList:(id)myData {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"mobile-watchlist.plist"];
[myData writeToFile:path atomically:YES];
}
+(NSMutableArray*)getDataFromPList
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"mobile-watchlist.plist"];
NSMutableArray* myArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSLog(#"READING -- PList Data Count: %d", [myArray count]);
return [[NSMutableArray alloc] initWithContentsOfFile:path];
}
For some reason mergedSymbolsArray will not write to a plist. I am not sure why?
I am able to write the following into a plist:
[tickers addObject:[NSDictionary dictionaryWithObjectsAndKeys:#"GOOG", #"symbol", #"2044", #"id", nil]];
[tickers addObject:[NSDictionary dictionaryWithObjectsAndKeys:#"AAPL", #"symbol", #"686", #"id", nil]];
[tickers addObject:[NSDictionary dictionaryWithObjectsAndKeys:#"YHOO", #"symbol", #"4177", #"id", nil]];
[Utils writeObjectToPList:tickers];
Why doesn't the first block of code write to a plist?
ADDITIONAL NOTES:
Here is an example of a dictionary the mergedSymbolsArray contains:
{
"charts_count" = 2;
"created_at" = "2010-04-12T16:37:32Z";
exchange = NASDAQ;
"followers_count" = 259;
id = 8404;
industry = "<null>";
"messages_count" = 1436;
ric = "GRPN.O";
sector = "<null>";
symbol = GRPN;
title = Groupon;
"updated_at" = "2011-09-05T04:17:56Z";
}
I am guessing the writeToFile:atomically method is failing because cannot be written?
Make sure all the objects you keep in the array are the property list objects (NSString, NSData, NSArray, or NSDictionary).
It fails because dta contains sector = "<null>" whose value is interpreted as NSNull and cannot be written to a plist.
I convert NSArray or NSDictionary to NSData before serializing. Following is a category on nsarray for serializing and deserializing. This comfortableby handles some data being nsnull
-(BOOL)writeToPlistFile:(NSString*)filename{
NSData * data = [NSKeyedArchiver archivedDataWithRootObject:self];
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString * path = [documentsDirectory stringByAppendingPathComponent:filename];
BOOL didWriteSuccessfull = [data writeToFile:path atomically:YES];
return didWriteSuccessfull;
}
+(NSArray*)readFromPlistFile:(NSString*)filename{
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString * path = [documentsDirectory stringByAppendingPathComponent:filename];
NSData * data = [NSData dataWithContentsOfFile:path];
return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
Check the return value of [myData writeToFile:path atomically:YES]; please. If it's NO, some errors have occurred while writing.Try to alloc-init the NSMutableArray instead of creating an autoreleased one.
Try adding a forward slash to your file name Ie:
NSString *path = [documentsDirectory stringByAppendingString:#"/mobile-watchlist.plist"];

Add to Favourites Function iPhone

I am trying to get an "add to favourites" feature working on my app. I just can't seem to get it working correctly. Basically everytime my phone restarts, all the favourites are deleted from the array and dictionary. Is there anyway to save this data so that it is kept and restored everytime the app launches? Many thanks.
Here is some of the code: in appDidFinishLaunching:
//============== Add To Favourites ==============
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"Saved.data"];
NSArray *pathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryArray = [pathsArray objectAtIndex:0];
NSString *filePathArray = [documentsDirectoryArray stringByAppendingPathComponent:#"savedArray.data"];
delegateFavouritesDictionary = [NSMutableDictionary dictionary];
[delegateFavouritesDictionary writeToFile:filePath atomically:YES];
delegateFavouritesArray = [[NSMutableArray alloc]init];
In the detailViewController viewDidLoad:
self.addToFavouritesArray = [[NSMutableArray alloc] init];
self.addToFavouritesDictionary = [NSMutableDictionary dictionary];
TabBar_NavigationBasedAppDelegate *mainDelegate = (TabBar_NavigationBasedAppDelegate *)[[UIApplication sharedApplication]delegate];
//addToFavouritesArray = [[NSMutableArray alloc] init];
NSMutableArray *tempArray1 = mainDelegate.delegateFavouritesArray;
//NSMutableDictionary *tempDictionary1 = mainDelegate.delegateFavouritesDictionary;
addToFavouritesArray = tempArray1;
//self.addToFavouritesDictionary = tempDictionary1;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"Saved.data"];
addToFavouritesDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
In the detailViewController, in the addToFavourites Function:
NSString *ID = [[NSUserDefaults standardUserDefaults]objectForKey:#"ID"];
if([[addToFavouritesDictionary allKeys] containsObject:ID]) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"Saved.data"];
[addToFavouritesDictionary removeObjectForKey:ID];
[addToFavouritesArray removeObject:Name];
[favouritesButton setTitle:#"+ Favourites" forState:(UIControlState)UIControlStateNormal];
[addToFavouritesDictionary writeToFile:filePath atomically: YES];
NSLog(#"New Dictionary: %#", addToFavouritesDictionary);
} else {
[addToFavouritesArray addObject:Name];
NSString *ID = [[NSUserDefaults standardUserDefaults]objectForKey:#"ID"];
[addToFavouritesDictionary setObject:Name forKey:ID];
[favouritesButton setTitle:#"- Favourites" forState:(UIControlState)UIControlStateNormal];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"Saved.data"];
[addToFavouritesDictionary writeToFile:filePath atomically: YES];
NSLog(#"Mutable Dictionary: %#", addToFavouritesDictionary);
//[addToFavouritesDictionary release];
}
In the FavouritesViewController, in viewDidLoad:
TabBar_NavigationBasedAppDelegate *mainDelegate = (TabBar_NavigationBasedAppDelegate *)[[UIApplication sharedApplication]delegate];
favouritesArray = [[NSMutableArray alloc] init];
NSMutableArray *tempArray1 = mainDelegate.delegateFavouritesArray;
favouritesArray = tempArray1;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"Saved.data"];
favouritesDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
Many thanks for any help
In your applicationDidFinishLaunching: method (so every time your app launches), you're first creating an empty NSMutableDictionary and then writing that to Saved.data, potentially overwriting anything that might have been there.

Reading BOOL from plist, writing over it with new value

I've established how to read a BOOL from a plist, but I don't know how to write over the BOOL value I originally read.
The BOOL I'm reading is buried within my plist. When the view loads I take a dictionary from the plist that contains all the info for the view (detail view at the end of a drill down), and the BOOL is inside that dictionary. I can change the value of the BOOL in the dictionary once it's been brought into the app, but I don't know how to write this back to the postion within the plist.
Hopefully this is making sense?
Cheers!
Write a NSDictionary to a Property List:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"FileName.plist"];
NSDictionary *myDict = [[NSDictionary alloc] init];
[myDict setObject:[NSNumber numberWithBool:YES] forKey:#"myKey"];
[myDict writeToFile:filePath atomically:YES];
Read the Boolean:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"FileName.plist"];
NSDictionary *myDict = [[NSDictionary alloc] initWithContentsOfFile:filePath];
BOOL myBool = [[myDict objectForKey:#"myKey"] boolValue];
You can also put the filePath code into this function:
- (NSString *)getFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"FileName.plist"];
return filePath;
}
And use it like this: (for reading)
NSDictionary *myDict = [[NSDictionary alloc] initWithContentsOfFile:[self getFilePath]];
BOOL myBool = [[myDict objectForKey#"myKey"] boolValue];
And this: (for writing)
NSDictionary *myDict = [[NSDictionary alloc] init];
[myDict setObject:[NSNumber numberWithBool:YES] forKey:#"myKey"];
[myDict writeToFile:[self getFilePath] atomically:YES];
For writing BOOL to .plist use below code
NSMutableDictionary* myDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
[myDict setObject:[NSNumber numberWithBool:YES] forKey:#"LSUIElement"];
[myDict writeToFile:plistPath atomically:NO];
Also have look at the post
Try this function:
-(void) setStringToPropertyList:(NSString*) pList withKey:(NSString*)key andValue:(NSString*)val{
//the path.
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:pList];
//read the dictionary.
NSMutableDictionary *temp = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
//modify the desired key of the dictionary.
[temp setValue:val forKey:key];
//rewrite the diccionary again.
if([temp writeToFile: plistPath atomically:YES]){
NSLog(#"setStringToPropertyList: pList updated");
}else{
NSLog(#"setStringToPropertyList: error writing the file");
}
//memory management
if(!temp) [temp release];
}
This function allows you to write properties to a pList, parameteres:
pList = name of the file
key = key of the property
val = value of the property
Hope it helps.

Not able to write to Plist

I have a very basic question about how to write to a plist. I added a "Here.plist" file to my resources folder. The following is my code:
NSString *path = [[NSBundle mainBundle]pathForResource:#"Here" ofType:#"plist"];
NSArray *array = [[NSArray alloc]initWithObjects:#"First", #"Second", #"Third", nil];
[array writeToFile:path atomically:YES];
"Here.plist" is still empty after executing the code. I even tried using a NSDictionary as follows:
NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:#"firstValue", #"First", #"secondValue", #"Second", #"thirdValue", #"Third", nil];
[dict writeToFile:path atomically:YES];
I went through many tutorials and forums but its just not working for me. What am i doing wrong? Losing my head over this.
You can't write into the bundle. Try writing into the documents directory. For example:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *array = [[NSArray alloc]initWithObjects:#"First", #"Second", #"Third", nil];
[array writeToFile:[documentsDirectory stringByAppendingPathComponent:#"Here.plist" atomically:YES];
I suspect your problem is due to the fact that you can't write to the application bundle, only to your application's document store.
To obtain a path to the document store, use:
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docStorePath = [searchPaths objectAtIndex:0];
NSString *filePath = [docStorePath stringByAppendingPathComponent:#"XXXXX"];

Writing to plists located at resource folder in iPhone

Any one please answer me, how to write into the plist file which is located in my resource folder. Please write an example code for me.
You can't. The application bundle is not writable on iPhone OS devices.
-(IBAction)textFieldCheck:(id) sender {
myPrimaryinfo = [[NSMutableArray arrayWithCapacity:6]retain];
NSArray *keys = [NSArray arrayWithObjects:#"Date",#"Time",#"Address",#"City",#"State",#"Zipcode",nil];
[myPrimaryinfo addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:
[NSString stringWithFormat:#"%#",txtDate.text],
[NSString stringWithFormat:#"%#",txtTime.text],
[NSString stringWithFormat:#"%#",txtAddress.text],
[NSString stringWithFormat:#"%#",txtCity.text],
[NSString stringWithFormat:#"%#",txtState.text],
[NSString stringWithFormat:#"%#",txtZip.text],nil]forKeys:keys]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"myPrimaryinfo.plist"];
[myPrimaryinfo writeToFile:path atomically:NO];
NSLog(#"PrimaryInfo array: %#", myPrimaryinfo);
}