简体   繁体   English

开始将NSUserDefaults用于自定义类时,这一次是要移至Core Data吗?

[英]when starting to use NSUserDefaults for a custom classes is this time to move to Core Data?

When starting to use NSUserDefaults for a custom classes, with multiple levels, is this time to move to Core Data? 当开始对具有多个级别的自定义类使用NSUserDefaults时,这次是否要移至核心数据?

Background: 背景:

  • I did start off using NSUserDefaults for my application data, which was basic 3 levels (see below for a rough example) 我确实开始将NSUserDefaults用于我的应用程序数据,这是基本的3个级别(请参见下面的大致示例)
  • I then wanted to have more readability/typing when using the structure so have started to create custom classes for the data 然后,我想在使用该结构时具有更高的可读性/类型,因此开始为数据创建自定义类
  • I'm now realizing these custom classes will need to implement initWithCoder and encodeWithCoder methods 我现在意识到这些自定义类将需要实现initWithCoder和encodeWithCoder方法

So given the above I'm really wondering whether I'd be better off going to Core Data here. 因此,鉴于以上所述,我真的很想知道我是否最好在这里使用Core Data。 Don't you conceptually here get to define the structure in XCode then have access saving/loading the data directly into the classes XCode creates for you? 您不是在概念上在XCode中定义结构,然后可以将数据直接保存/加载到XCode为您创建的类中吗?

Rough examples of structure: 结构的粗略示例:

  • Items ( NSMutableArray ) 物品( NSMutableArray
    • aString: NSString aString:NSString
    • aString2: NSString aString2:NSString
    • aDate: NSDate 日期:NSDate
    • aDate2: NSDate aDate2:NSDate
    • aBool: BOOL aBool:布尔
    • aTI1: NSTimeInterval aTI1:NSTimeInterval
    • aTI2: NSTimeInterval aTI2:NSTimeInterval
    • Keywords ( NSMutableArray ) 关键字( NSMutableArray
      • keyword: NSString 关键字:NSString
      • keyword: NSString 关键字:NSString

Core Data or property lists or straight up object archival, yes. 是的,核心数据或属性列表或直接的对象归档。 As soon as you have any kind of data beyond very simple key/value pairs, you need to think about your model layer. 一旦拥有非常简单的键/值对之外的任何类型的数据,就需要考虑模型层。

Convenient though it is, user defaults -- as the name implies -- just isn't a place to persist user data! 尽管很方便,但用户默认值(顾名思义)不是保留用户数据的地方! It is for configuration, preferences, etc... 它用于配置,首选项等。

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

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