Quote:
Originally Posted by newguy2010
I am using nsxml parser to parse google weather Api.I am storing my contents into an NSdictionary.I just want to do couple of things..
1.add system date at the nsdictionary at index 0.
2.Increment that day by each index .
3.Sort my parser by dates.....
|
Dictionaries don't have indexes. They have keys. Arrays have indexes.
Thus, you can't exactly save the system date into a dictionary "at index 0".
You need to decide what format you want to use to save your dates. You can save NSDate objects directly into a dictionary or array, and they will even save into Cocoa plist objects. However, if you want to save dates to XML, or upload them to a server, it's probably better to convert them to a number.
If you wanted to save a date a new key to a dictionary, you could do it like this:
To save a date to a dictionary as an NSDate object, do this:
Code:
int index = 0;
NSDate* today = [NSDate date];
NSString* indexString = [NSString stringWithFormat: @"%d", index];
[dateDictionary setObject: today forKey: indexString];
To save it as an integer value that is the number of seconds since Jan 1, 2001, do this instead:
Or to save it as an integer number of seconds since midnight on 01 Jan 1 2001, do this:
Code:
int index = 0;
NSDate* today = [NSDate date];
unsigned long dateInt = [today timeIntervalSinceReferenceDate];
NSNumber* dateNumber = [NSNumber numberWithInt: dateInt];
NSString* indexString = [NSString stringWithFormat: @"%d", index];
[dateDictionary setObject: dateNumber forKey: indexString];
If you need more precision, you can convert the date to a double, and use the NSNumber method numberWithDouble. However, single-second precision is fine for most applications.