Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

rewriting dates

I’m having some trouble changing the dates in an array of dictionary objects. The basic code is below.

The contents of arrayOfNewsItems checks out fine – I use this loop to check beforehand:

// check items before
for (int t=0; t<[arrayOfNewsItems count]; t++){
NSLog(@\"Items before: %@\",[[arrayOfNewsItems objectAtIndex:t] objectForKey:@\"pubDate\"]);
}


Then I make some changes to the date field – removing some unwanted letters:

// Now loop through and check for weird characters in the date, and remove them
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
NSString *currentDate = [[NSString alloc] init];

for (int i=0; i<[arrayOfNewsItems count]; i++){
NSLog(@\"Date before: %@\",[[arrayOfNewsItems objectAtIndex:i] objectForKey:@\"pubDate\"]);
[tempDict setDictionary:[arrayOfNewsItems objectAtIndex:i]];
currentDate = [tempDict objectForKey:@\"pubDate\"];
currentDate = [currentDate stringByReplacingOccurrencesOfString:@\"T\" withString:@\" \"];
currentDate = [currentDate stringByReplacingOccurrencesOfString:@\" ue\" withString:@\"Tue\"];
currentDate = [currentDate stringByReplacingOccurrencesOfString:@\" hu\" withString:@\"Thu\"];
currentDate = [currentDate stringByReplacingOccurrencesOfString:@\"GM \" withString:@\"GMT\"];
currentDate = [currentDate stringByReplacingOccurrencesOfString:@\"ED \" withString:@\"EDT\"];
currentDate = [currentDate stringByReplacingOccurrencesOfString:@\"Z\" withString:@\" \"];
[tempDict removeObjectForKey:@\"pubDate\"];
[tempDict setValue:currentDate forKey:@\"pubDate\"];
[arrayOfNewsItems replaceObjectAtIndex:i withObject:tempDict];
NSLog(@\"Date after: %@\",[[arrayOfNewsItems objectAtIndex:i] objectForKey:@\"pubDate\"]);
}// end for


According to my debug statements within the loop, my changes are working for that section also. But, when I check the values afterwards (using the loop below), the dates are all the same.

// check items after
for (int r=0; r<[arrayOfNewsItems count]; r++){
NSLog(@\"Items after: r= %i %@\",r,[[arrayOfNewsItems objectAtIndex:r] objectForKey:@\"pubDate\"]);
}


What am I missing, or misusing?
Post edited by pereirap on

Replies

  • FstuffFstuff Posts: 154Registered Users
    You're abusing NSMutableDictionary for one. Or at least, it's not doing what you think it's doing. As best I can tell, you are trying in your for-loop to iterate through an array of dictionaries, and replace each dictionary with an updated copy of each entry. It seems that you are trying to accomplish this by calling [NSMutableDictionary setDictionary]. However, you are still dealing with the same NSMutableDictionary object on each iteration. The value of 'tempDict' is set by this line:
    NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
    But you never create any other NSMutableDictionary objects nor do you pull any NSMutableDictionary objects out of your array. Therefore you are pointing each index of your array to the same NSMutableDictionary object.

    I'm also not sure what you are trying to accomplish with the dates but it seems unwieldy. Check out Apple's Time and Date programming guide, it might help you find a better way to accomplish your goal.

    Finally, a suggestion: Use the Code tag to format your code. Preserves spacing/indentation and is typically easier to read.
Sign In or Register to comment.