保存日期和值的正确结构 [英] Proper Structure for Saving Date and Value

查看:23
本文介绍了保存日期和值的正确结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试保存用户信息并将其显示到 UITableViewCell 中:

I am trying to save user information and display it into a UITableViewCell as:

(日期)-(值)

喜欢:

(12/12/2014) - (240 磅)

(12/12/2014) - (240lbs)

我不确定要使用什么结构类型.我需要保存 2 个不同的对象(上面描述的 nsdate 和 nstring 值),并且能够通过键(日期和值)遍历它们以在 uitableview 中显示每个对象的文本.

I'm unsure of what structure type to use. I need to save 2 different objects (the nsdate and nstring values described above) and be able to iterate through them by key (date and value) to display the text for each within the uitableview.

推荐答案

NSDictionary 这不是您要找的吗?示例:

Isn't NSDictionary what you're looking for? Example:

NSDictionary *myKeys = [[NSDictionary alloc] initWithObjectsAndKeys:
                            [NSDate date], @"date",
                            [NSNumber numberWithUnsignedInt:240], @"weight",
                            nil];

注意对象和键"的顺序.这里我使用 @"date"@"weight" 作为键名.然后您可以通过执行以下操作访问对象:

Pay attention to the order "Objects and Keys". Here I used @"date" and @"weight" as the keys names. Then you can access the objects by doing:

myKeys[@"weight"]

myKeys[@"date"]

如果你想有多个与日期和体重相关的条目,你可以使用 NSDictionariesNSArray.

And if you want to have multiple entries relating date and weight, you can use an NSArray of NSDictionaries.

在您提供更多上下文后更新:如果用户需要记录某些内容,则可以使用 Core Data.核心数据实体的工作方式与 NSDictionary 非常相似,但它们实际上是包含在 NSManagedObjectContext 中的 NSManagedObject.您应该创建一个具有两个属性的实体:日期和重量.

Updated after you provided more context: if the user needs to log something, Core Data is the way to go. The Core Data entities work very similarly to NSDictionary's but they will actually be NSManagedObject's contained inside an NSManagedObjectContext. You should create an entity with two properties: date and weight.

这篇关于保存日期和值的正确结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆