Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 3    Views: 73

NCbuzz
Code is Art
Profile
Posts: 575
Reg: Sep 11, 2013
Lillington, NC
11,100
02/26/15 03:17 PM (9 years ago)

Problems loading array that was saved to device - Solved FYI

I am saving out arrays with the following method. (I have debugged and verified that NSArray contains all of the items.) //==== saving file ========================= GO - (void)saveArrayToPhone: (NSArray *)myArray { NSLog(@"array that we are saving= %@", myArray); // debug //save the array NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; [myArray writeToFile:documentsDirectory atomically:YES]; } The name of the array I passed in was: emailListForSend ===================== My problem is that I am not clear on path and filename to load back in/ The following is what I tried which loads a null array :( ===================== //==== loading file =========================NoGo NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString* path = [documentsDirectory stringByAppendingPathComponent: @"emailListForSend"] ; NSLog(@"path including array name:%@", path ); //debug NSArray* toRecipients = [NSArray arrayWithContentsOfFile:path]; NSLog(@"toRecipients array contains:%@", toRecipients ); //debug ==================== obviously learning arrays. one of the items that I am hoping is covered in CodeCrush Any advice on how to mod the load portion would be appreciated. Thanks.
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
02/26/15 07:10 PM (9 years ago)
I'm not good enough to correct your code, but I can say that my code works, and is fairly simple... NSError *error; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self.childArray options:NSJSONWritingPrettyPrinted error:&error]; NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; NSString *tmpChildItems = [NSString stringWithFormat:@"{\"childItems\":%@}",jsonString]; [BT_fileManager saveTextFileToCacheWithEncoding:tmpChildItems fileName:@"yourFile.txt" encodingFlag:-1]; Basically what I'm doing is grabbing childItems from different files, and creating a new childItem file. "childArray" is an NSMutableArray, but you can use any array. From there, you can read that file from the cache: [BT_fileManager readTextFileFromCacheWithEncoding:@"yourFile.txt" encodingFlag:-1] This has been cut from various methods and pieces of code I'm working with, but it 'should' work, or at least be close enough to get you going. I hope, lol! If not, yell. Cheers! -- Smug
 
NCbuzz
Code is Art
Profile
Posts: 575
Reg: Sep 11, 2013
Lillington, NC
11,100
like
02/26/15 08:43 PM (9 years ago)
Thanks. Interesting... The array that I passed into the saveArrayToPhone was a MSMutableArray This was a pretty good tutorial if anyone else is looking for a quick overview: http://www.rypress.com/tutorials/objective-c/data-types/nsarray Lots of good info for adding, deleting, sorting etc.
 
NCbuzz
Code is Art
Profile
Posts: 575
Reg: Sep 11, 2013
Lillington, NC
11,100
like
03/01/15 09:42 AM (9 years ago)
In case anyone else runs into similar situation. My mistake actually was in the save & load routines with not having a full path including filename created correctly. The code below has been corrected (and works :) //==== saving file ========================= - (void)saveArrayToPhone: (NSMutableArray *)myArray { NSLog(@"array that we are saving= %@", myArray); // debug //save the array NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:"nameOfMyFile"]; // addded correct fullPath [myArray writeToFile:fullPath atomically:YES]; // used fullPath } //==== loading file ========================= NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString* fullPath = [documentsDirectory stringByAppendingPathComponent: @"nameOfMyFile"] ; NSLog(@"path including array name:%@", fullPath ); //debug NSMutableArray* toRecipients = [NSArray arrayWithContentsOfFile:fullPath]; NSLog(@"toRecipients array contains:%@", toRecipients ); //debug ==================== actually posted something that wasn't a question ;)
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.