将数组嵌套到 NSDictionary 对象中 (Objective-C)
我想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是指
NSNumber
的+numberWithInt:
和+numberWithBool:
方法,而不是NSDate
吗? NSNumber 定义了这些方法。要将数字转换为日期,实际上取决于数字是什么。如果它是 UNIX 时间戳,则为
[NSDate dateWithTimeIntervalSince1970:theTimestampInSeconds]
。至于将
NSArray
对象添加到字典中:如果您需要更多帮助,则必须发布一些代码,因为您的问题不是很清楚。
Do you mean
NSNumber
's+numberWithInt:
and+numberWithBool:
methods, notNSDate
? 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:You'll have to post some code if you need more help as your question isn't very clear.
对于日期,您还可以使用
numberWithInt:
和 NSDate 的timeIntervalSince1970
方法来获取时间戳。该时间戳还有一个构造函数。对于嵌套数组,您可以简单地在字典中添加 NSArray 对象。
for the date you can also use
numberWithInt:
and thetimeIntervalSince1970
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.
您的 plist 应该选择日期格式并使用 Date 类型存储它,这样您就可以直接插入
NSDate
中。要将数组保存到字典中,请使用以下内容:
Your plist should pick up the date format and store it using the Date type, so you can just ploug in the
NSDate
.To save an array to your dictionary, use something like: