开发者

iOS - what should I use for saving data

Imagine you have a list of items which is an array of objects (fields: title, description, date) ordered by date. You pick an item from the list开发者_C百科 and store it on the local device. You then load a list of stored items in an array (UITableView) ordered by date.

I think Using Core Data is overkill. What would you use and how? THx!


Well I would make the Class implement NSCoding and save the object to file using NSKeyedArchiver;

You can just loop thru the directory and load all the object with NSKeyedUnarchiver.


If you do not have too many items in your array, using some kind of xml solution would be perfect.

plists are great because the back end storage is human readable and editable. Also there are inbuilt feature to effortlessly turn dictionaries and arrays into plists. e.g. (writeToFile:atomically:) and (arrayWithContentsOfFile:)

Note you can only use these methods if your array / dictionaries contain only the following items: NSString, NSData, NSArray, or NSDictionary. Otherwise you will have to implement NSCoding which is slightly more work.

COre data has many benefits, so if you loading / searching is taking time, consider switching.


Well you have an option of SQLite and Plist too...

i would prefer SQLite as the sorting of data would be easier..


Use PList its the best and easiest way to save data in iphone application. i will tell you how to do that.

this is the way to save your data in Plist.

//Create String to get data ... 

NSString *typeUrl = [NSString stringWithFormat:@"%@",url.text];

// Create Plist File

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"url.plist"];

NSString *tempurl = [NSString stringWithFormat:@"%@",typeUrl];

// Remove data previously stored in plist file ... 

[[NSFileManager defaultManager]  removeItemAtPath:path error:NULL];

// Create NSMutableArray to Store your string ... 

NSMutableArray urlDetails = [[NSMutableArray alloc] init];  

// Add String to Array ... 
[urlDetails addObject:tempurl];

// Store that array in Plist File ... 

[urlDetails writeToFile:path atomically:YES];

this is the way to read data from Plist.

// Find Plist file you created and cheack that file exsist ... 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"password.plist"];
    BOOL passwordfileExists = [[NSFileManager defaultManager] fileExistsAtPath:path];

    // if the file exsist create NSMutableArray and read the value and put that in to the String

    if (passwordfileExists == YES) {
        NSMutableArray* plistDict = [[NSMutableArray alloc] initWithContentsOfFile:path];
        NSString *value = [NSString stringWithFormat:@"%@",[plistDict objectAtIndex:0]];

        // Set that string to the textField

        oldpassText = [NSString stringWithFormat:@"%@",value];
    }

This is how I save data in Plist file and its soo much easy. I think this will help you .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜