IPhone SDK: Checking the ID number for each object in the UITableView and changing the icon number accordingly
I am trying to use some code to create an icon number for an iPhone App Icon.
I can do this successfully, however the code I am using (below) is quite bad and could easily be done better, however I have no logical knowledge to do this.
This is what he does ...
- Get user id from first post in UITableView. Then it checks this against the stored id (from the first object when it was last loaded) and makes the icon 1.
- It then checks the second ID in the message to see if it matches the one stored. If not, icon 2 will do it.
- etc. etc.
What I want to do is automatically check for IDs and add "1" to the icon number until the first column with the correct ID is found, I'm not too sure about the coding.
Any help would be great!
thanks
(Code below :)
NSDictionary* tweetValues = [[NSDictionary alloc] init];
tweetValues = [tweets objectAtIndex:1];
NSString *newStatusID = [[tweetValues objectForKey:@"id"] stringValue];
NSString *oldStatusID = [[NSUserDefaults standardUserDefaults] stringForKey:@"latestID"];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
if([newStatusID isEqualToString:oldStatusID]){
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
} else {
tweetValues = [tweets objectAtIndex:2];
newStatusID = [[tweetValues objectForKey:@"id"] stringValue];
if ([newStatusID isEqualToString:oldStatusID]){
[UIApplication sharedApplication].applicationIconBadgeNumber = 1;
}else{
tweetValues = [tweets objectAtIndex:3];
newStatusID = [[tweetValues objectForKey:@"id"] stringValue];
if ([newStatusID isEqualToString:oldStatusID]){
[UIApplication sharedApplication].applicationIconBadgeNumber = 2;
}else{
[UIApplication sharedApplication].applicationIconBadgeNumber = 3;
}
}
}
a source to share
I'm not really sure what you are trying, but looking at your code I think you need to use a for loop.
Also the first line of your code is a leak. I'm not sure what you are trying to do. As far as I can see, you don't need to create an empty dictionary here.
The line where you retrieve your tweetValues ( tweetValues = [tweets objectAtIndex:1];
) is also weird. You are returning the object at index 1. Since the arrays are zero-indexed , you are retrieving the second object in the array. If you want to receive the first message, you need to call:[tweets objectAtIndex:0];
If I had to refactor your code, I would probably do something like:
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
NSString *oldStatusID = [[NSUserDefaults standardUserDefaults] stringForKey:@"latestID"];
for (NSDictionary *tweetValues in tweets) { // Iterate over all the tweetValues in tweets (assume tweets exist?)
NSString *newStatusID = [[tweetValues objectForKey:@"id"] stringValue];
if ([oldStatusID isEqualToString:newStatusID]) {
[UIApplication sharedApplication].applicationIconBadgeNumber++; // Increase the badge number by 1
}
}
I hope I interpreted your code correctly. If not, another context may be helpful.
a source to share
I ended up with a job that seems to work:
NSDate *dateRefreshed = [[NSUserDefaults standardUserDefaults] objectForKey:@"dateRefreshed"];
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"dateRefreshed"];
[[NSUserDefaults standardUserDefaults] synchronize];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
for (NSDictionary *tweetValues in tweets) { // Iterate over all the tweetValues in tweets (assume tweets exist?)
NSDate *dates = [tweetValues objectForKey:@"created_at"];
NSTimeInterval sincePostDate = [[NSDate date] timeIntervalSinceDate: dates];
NSTimeInterval sinceNow = [[NSDate date] timeIntervalSinceDate: dateRefreshed];
int timeDiff = sincePostDate;
int timeSinceNow = sinceNow;
if (timeDiff <= timeSinceNow){
[UIApplication sharedApplication].applicationIconBadgeNumber++; // Increase the badge number by 1
}
}
If anyone has any better ideas, feel free to post the code :)
a source to share