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

发布于 2024-11-27 18:01:59 字数 152 浏览 0 评论 0原文

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

我认为使用核心数据有点矫枉过正。您会使用什么以及如何使用? 谢谢!

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 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

后eg是否自 2024-12-04 18:01:59

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

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

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.

懒猫 2024-12-04 18:01:59

如果数组中没有太多项目,那么使用某种 xml 解决方案将是完美的。

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

请注意,如果您的数组/字典仅包含以下项目,则只能使用这些方法:NSString、NSData、NSArray 或NS字典。否则,您将必须实现 NSCoding 工作量稍微大一些。

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

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.

夢归不見 2024-12-04 18:01:59

好吧,你也可以选择 SQLitePlist...

我更喜欢 SQLite,因为数据排序会更容易。

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

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

缱倦旧时光 2024-12-04 18:01:59

使用 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 文件中保存数据的方法,它非常简单。我想这会对你有所帮助。

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 .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文