将数据保存到 iOS 的最佳方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11072455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:50:02  来源:igfitidea点击:

Best way to save data to iOS?

iosxcodeios5save

提问by H3rrVorr4g3nd

In my application (iOS 5) I want to save data - I want to save debts. So its:

在我的应用程序(iOS 5)中,我想保存数据 - 我想节省债务。所以是:

  • plus or minus money
  • the amount of money
  • and the name who has the debts (or the name where you have the debts)
  • 加减钱
  • 金额
  • 和欠债的名字(或你欠债的名字)

But I don't how to save the data (NSUserdefaults,Core data, SQLLite)

但我不知道如何保存数据(NSUserdefaults、Core data、SQLLite)

Maybe you can tell me the best way to save them?

也许你能告诉我拯救他们的最好方法?

回答by Yarlik

The easiest way to store small amount of data on your device is to use NSUserDefaults. But only property lists could be saved in this way. A property list is a combination of objects of 6 types, NSNumber, NSString, NSArray, NSDictionary, NSDate, NSData. In your case it's easy to do. For example, to save a new debt record you can use following method:

在设备上存储少量数据的最简单方法是使用 NSUserDefaults。但只有属性列表才能以这种方式保存。属性列表是 6 类对象的组合,NSNumber、NSString、NSArray、NSDictionary、NSDate、NSData。在你的情况下,这很容易做到。例如,要保存新的债务记录,您可以使用以下方法:

#define DEBTS_LIST_KEY @"listOfAllDebts"
#define DEBTOR_NAME_KEY @"debtorName"
#define DEBT_AMOUNT_KEY @"amountOfDebt"

-(void) saveDebt:(CGFloat) debtAmount forName:(NSString *) debtorName
{
    // pointer to standart user defaults
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    // the mutalbe array of all debts
    NSMutableArray * alldebtRecords = [[defaults objectForKey:DEBTS_LIST_KEY] mutableCopy];
    // create new record
    // to save CGFloat you need to wrap it into NSNumber
    NSNumber * amount = [NSNumber numberWithFloat:debtAmount];

    NSDictionary * newRecord = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:amount,debtorName, nil] forKeys:[NSArray arrayWithObjects:DEBT_AMOUNT_KEY, DEBTOR_NAME_KEY, nil]];
    [alldebtRecords addObject:newRecord];
    [defaults setObject:alldebtRecords forKey:DEBTS_LIST_KEY];
    // do not forget to save changes
    [defaults synchronize];
}

To readList of debts you have read something similar.

要阅读债务清单,您已经阅读了类似的内容。

But I recommend you to use core data. It's more flexible and you won't have to write all this code to manage your data (to edit existed records, or to delete them). You will be able to extend your model much easier, for example, when you want to save the date of the debt. This is the link to a good tutorial

但我建议您使用核心数据。它更灵活,您不必编写所有这些代码来管理您的数据(编辑现有记录或删除它们)。您将能够更轻松地扩展您的模型,例如,当您想要保存债务日期时。这是一个很好的教程的链接

回答by Chris Trahey

If the quantity of records is user-defined, and will grow with app use, I suggest Core Data, which can be backed by SQLite. If you are working in a modern Xcode (i.e. Xcode 4), creating models is easy and graphical. If you have ever worked with ORM frameworks before, the interface for querying, etc. should be easy to grasp.

如果记录的数量是用户定义的,并且会随着应用程序的使用而增长,我建议使用 Core Data,它可以由 SQLite 提供支持。如果您使用的是现代 Xcode(即 Xcode 4),则创建模型非常简单且图形化。如果您以前使用过 ORM 框架,那么查询等接口应该很容易掌握。

Search around for some tutorials, but be specific about finding tutorials that match your version of Xcode, as Core Data development has been changing a lot lately.

四处搜索一些教程,但要具体寻找与您的 Xcode 版本相匹配的教程,因为 Core Data 开发最近发生了很大变化。