Nesting arrays into NSDictionary object (Objective-C)

醉酒当歌 提交于 2020-01-16 18:58:09

问题


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!


回答1:


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.




回答2:


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.




回答3:


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"];


来源:https://stackoverflow.com/questions/4582791/nesting-arrays-into-nsdictionary-object-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!