将数组嵌套到 NSDictionary 对象中 (Objective-C)

发布于 2024-10-10 02:07:02 字数 419 浏览 4 评论 0原文

我想使用 NSDictionary 定义任务,我想将其保存在 plist 文件中(到目前为止,我在 Core Data 方面运气不佳),但在两点上陷入困​​境:

-- 使用 initWithObjectsAndKeys 时: 我可以分别使用 NSDate 的 numberWithInt:numberWithBool: 方法将数据类型更改为数字或布尔值。不过,我似乎找不到迄今为止更改类型的方法。我在文档中找不到类似的内容。

-- 我遇到的第二个问题是嵌套数组。我怎样才能将它们添加到字典中?

我已将我想要实现的目标的图片上传到此处。先感谢您!

I would like to define tasks using NSDictionary, which I'd like to save in a plist file (I didn't have much luck with Core Data so far), but got stuck at two points:

-- When using initWithObjectsAndKeys: I can change the data type to number or boolean, using NSDate's numberWithInt: and numberWithBool: methods, respectively. I can't seem to find the method to change the type to date though. I couldn't find anything like that in the documentation.

-- The second problem I ran into was with the nested arrays. How can I add them to the dictionary?

I have uploaded a picture to here of what I am trying to achieve. Thank you in advance!

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

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

发布评论

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

评论(3

写下不归期 2024-10-17 02:07:02

您是指 NSNumber+numberWithInt:+numberWithBool: 方法,而不是 NSDate 吗? NSNumber 定义了这些方法。

要将数字转换为日期,实际上取决于数字是什么。如果它是 UNIX 时间戳,则为 [NSDate dateWithTimeIntervalSince1970:theTimestampInSeconds]

至于将 NSArray 对象添加到字典中:

[yourDictionary setObject:yourArray forKey:@"SomeKey"];

如果您需要更多帮助,则必须发布一些代码,因为您的问题不是很清楚。

Do you mean NSNumber's +numberWithInt: and +numberWithBool: methods, not NSDate? It's NSNumber that defines those methods.

To convert a number to a date, it really depends on what the number is. If it's a UNIX timestamp, then [NSDate dateWithTimeIntervalSince1970:theTimestampInSeconds].

As for adding NSArray objects to a dictionary:

[yourDictionary setObject:yourArray forKey:@"SomeKey"];

You'll have to post some code if you need more help as your question isn't very clear.

凑诗 2024-10-17 02:07:02

对于日期,您还可以使用 numberWithInt: 和 NSDate 的 timeIntervalSince1970 方法来获取时间戳。该时间戳还有一个构造函数。

对于嵌套数组,您可以简单地在字典中添加 NSArray 对象。

for the date you can also use numberWithInt: and the timeIntervalSince1970 method of NSDate to get a Timestamp. Theres also a Constructor for that Timestamp.

For nested Arrays you can simply add NSArray Objects in the Dictonary.

毁我热情 2024-10-17 02:07:02

您的 plist 应该选择日期格式并使用 Date 类型存储它,这样您就可以直接插入 NSDate 中。

[plist setObject:theDate forKey:@"theDateKey"];

要将数组保存到字典中,请使用以下内容:

[NSDictionary dictionaryWithObjectsAndKeys:@"1", @"One", @"2", @"Two"];

Your plist should pick up the date format and store it using the Date type, so you can just ploug in the NSDate.

[plist setObject:theDate forKey:@"theDateKey"];

To save an array to your dictionary, use something like:

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