Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For very small amounts of data, like a few strings, but not an image, you could also use the much simpler NSUserDefaults. This is usually for saving preferences or some persistent data.</p> <p>To save it:</p> <pre><code>[[NSUserDefaults standardUserDefaults] aString forKey:@"aStringKey]; if (![[NSUserDefaults standardUserDefaults] synchronize]) NSLog(@"not successful in writing the default prefs"); </code></pre> <p>To read it:</p> <pre><code>aString = [[NSUserDefaults standardUserDefaults] stringForKey:@"aStringKey]; </code></pre> <p>Again, I wouldn't go ahead and use this if what you really need is a database or a filesystem.</p> <p>If you are putting in more data, and not enough to warrant an SQL database or Core Data, then I would write the data to a plist file. If all your data are objects, then I would do this:</p> <pre><code> NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *path = [documentsDirectory stringByAppendingPathComponent:kSavedDocFileName]; NSMutableArray *myArrayToSave = [[NSMutableArray alloc] arrayWithCapacity: 48]; // 48 since you said in the comments that you have 48 of these for (int i = 0; i &lt; 48; i++) { NSDictionary *myDict = [[NSDictionary alloc] initWithObjectsAndKeys: sting1[i], @"firstStr", //I'm assuming your strings are in a C array. string2[i], @"secondStr",// up to you to replace these with the right url[i], @"myURL", //way to access your 48 pieces of data nil]; [myArrayToSave addObject:myDict]; [myDict release]; } if (![myArrayToSave writeToFile:path atomically:YES]) NSLog(@"not successful in saving the unfinished game"); </code></pre> <p>And you can read the data:</p> <pre><code> NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *path = [documentsDirectory stringByAppendingPathComponent:kMySavedDocName]; NSArray *myLoadedData = [[NSArray alloc] initWithContentsOfFile:path]; for (int i = 0; i &lt; 48; i++) { NSDictionary *myDict = [myLoadedData objectAtIndex:i]; string1[i] = [myDict objectForKey:@"firstStr"]; string2[i] = [myDict objectForKey:@"secondStr"]; url[i] = [myDict objectForKey:@"url"]; } [myLoadedData release]; </code></pre> <p>Make sure you don't just copy and paste this code since I just typed it in off the top of my head.</p> <p>Now, if you don't want to mess with a dictionary, you could do that too. It's a little less elegant:</p> <p>Save:</p> <pre><code> NSMutableArray *myArrayToSave = [[NSMutableArray alloc] arrayWithCapacity: 48 * 3]; // 48 since you said in the comments that you have 48 of these three objects for (int i = 0; i &lt; 48; i++) { [myArrayToSave addObject:string1[i]]; [myArrayToSave addObject:string2[i]]; [myArrayToSave addObject:url[i]]; } if (![myArrayToSave writeToFile:path atomically:YES]) NSLog(@"not successful in saving the unfinished game"); </code></pre> <p>Load:</p> <p>NSArray *myLoadedData = [[NSArray alloc] initWithContentsOfFile:path];</p> <pre><code>for (int i = 0; i &lt; 48; i++) { string1[i] = [myLoadedData objectAtIndex:i * 3]; string2[i] = [myLoadedData objectAtIndex:i * 3 + 1]; url[i] = [myLoadedData objectAtIndex:i * 3 + 2]; } [myLoadedData release]; </code></pre> <p>Another important note is that in all these examples, I am assuming that you are dealing with <em>objects</em> (48 times 3 of them). That is why you can just add them to dictionaries and arrays and save them and reload them easily. If you were dealing with non-objects, like ints, or c strings or BOOLs, then you will have to use [NSNumber ...] to turn those into objects and then [object ...] to turn them back into non-ojects to begin with. </p> <p>Try some of these, and then go find your app folder in ~/Library/Application Support/iPhone Simulator/Applications folder, and look in the Documents folder, open the file you have saved, and verify its contents. It's always reassuring to be able to check the data you write out - and to see how the different methods change the contents.</p> <p>Good luck!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload