Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Personally, rather than using NSUserDefaults which is fine for simple data, I would look into using the NSCoding protocol and NSKeyedArchiver. Have your objects implement the NSCoding protocol and then use something like this to archive/unarchive them:</p> <pre><code>+ (NSObject *)readArchiveFile:(NSString *)inFileName { NSFileManager *fileMgr = [NSFileManager defaultManager]; NSString *documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectoryPath, inFileName]; NSObject *returnObject = nil; if( [fileMgr fileExistsAtPath:filePath] ) { returnObject = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath]; } return returnObject; } + (void)archiveFile:(NSString *)inFileName inObject:(NSObject *)inObject { NSString *documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectoryPath, inFileName]; BOOL didSucceed = [NSKeyedArchiver archiveRootObject:inObject toFile:filePath]; if( !didSucceed ) { NSLog(@"File %@ write operation %@", inFileName, didSucceed ? @"success" : @"error" ); } } + (void)deleteFile:(NSString *)inFileName { NSFileManager *fileMgr = [NSFileManager defaultManager]; NSString *documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectoryPath, inFileName]; NSError *error; if ( [fileMgr fileExistsAtPath:filePath] &amp;&amp; [fileMgr removeItemAtPath:filePath error:&amp;error] != YES) { NSLog(@"Unable to delete file: %@", [error localizedDescription]); } } </code></pre> <p>Also, I would look into using CoreData as it gives you the ability to query your data.</p>
 

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