繁体   English   中英

iOS - 我应该用什么来保存数据

[英]iOS - what should I use for saving data

想象一下,您有一个项目列表,它是按日期排序的对象数组(字段:标题,描述,日期)。 您从列表中选择一个项目并将其存储在本地设备上。 然后,您可以按日期排序的数组(UITableView)中加载存储项的列表。

我认为使用核心数据是过度的。 你会用什么以及如何使用? 谢谢!

好吧,我会让类实现NSCoding并使用NSKeyedArchiver将对象保存到文件中;

您可以直接遍历目录并使用NSKeyedUnarchiver加载所有对象。

如果您的阵列中没有太多项目,使用某种xml解决方案将是完美的。

plists很棒,因为后端存储是人类可读和可编辑的。 还有内置功能,可以毫不费力地将字典和数组转换为plists。 例如( writeToFile:atomically:)和( arrayWithContentsOfFile :)

请注意,如果您的数组/字典仅包含以下项目,则只能使用这些方法:NSString,NSData,NSArray或NSDictionary。 否则你将不得不实现更多的工作NSCoding

COre数据有许多好处,因此如果您加载/搜索需要时间,请考虑切换。

那么你也有SQLitePlist的选择......

我更喜欢SQLite因为数据的排序会更容易..

使用PList是在iphone应用程序中保存数据的最佳和最简单的方法。 我会告诉你如何做到这一点。

这是在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];

这是从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];
    }

这就是我在Plist文件中保存数据的方式,而且它很容易。 我想这会对你有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM