Storing arbitrary keys/values in iOS

Viewed 4241

Is there a way to store arbitrary keys/values in iOS, I know I could create column that store JSON key-value in SQLite but I want something more efficient so I could be able to get only the values that I need from a queried record.

Preferably If I could do this without having external database (like LevelDB) but if you think this might be the only way, let me know which one you prefer.

Update:

The data will hold articles (long html articles, title and date, and other undefined data) retrieved from the server and other meta data, it will be retrieved and displayed every time the user opens the app. (thats why NSUserDefaults will not work for this use case)

7 Answers

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:

+ (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] && [fileMgr removeItemAtPath:filePath error:&error] != YES)
    {
        NSLog(@"Unable to delete file: %@", [error localizedDescription]);
    }
}

Also, I would look into using CoreData as it gives you the ability to query your data.

Look at NSUserDefaults.

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

You have NSUserDefaults and there you can store information.

Have only a few things to retain: 1 - If you do not leave the app is necessary to do synchronize to store the information; 2 - You can store and get and edit you information always

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:self.emailToSend forKey:@"teste.email"];
[defaults synchronize];


NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[alert textFieldAtIndex:0].text = [defaults objectForKey:@"teste.email"];
Related