Prismatic Core Failure We Require Assistance!

I thought I’d get into the habit of updating this blog weekly, just so that I can really monitor how much I get done. (Could be good, could be sad… could be really sad… we’ll see)
Onto the guts…
Have you ever felt like you were stuck on a problem for a while and the solution was right under your nose? This happens to me more than I’d like to admit. In fact it happened just earlier this week. Probably why I’m writing about it. Anyway, the situation goes as follows:

  • I’m programming an app that displays a list of people from your social network in a pretty table view with custom cells.
  • I want to organize these people alphabetically.
  • The request I send to the social network’s server returns all the “friends” in order of their ID which I presume is based on when they created their account.
  • I decide to create an NSArray to sort the names and then use that array to create an NSDictionary matching names with ID’s, respectively for the keys and values. (So Tim Cool is paired with 887525117) <–This is my Facebook ID for those wondering.
  • I then realize that in the unlikely case someone has two friends with the same name, I will have created the same key with different values! …F*CK
  • The solution (which took me much longer than it should’ve) is to obviously append the ID to the name, and then remove it when it’s displayed.
Code Within:
"- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView"

(note: I’m using an object “Social Friend” to store the name, picture, info, etc. – so the implementation won’t exactly match what I said above but you get the idea.)



//Create initial dictionary with full names tied to friend ID's.

NSMutableDictionary *friendDictionary = [[NSMutableDictionary alloc] init]; 

//This will be an all strings array

NSMutableArray		*fullNames = [[NSMutableArray alloc] init];

for (SocialFriend *f in friends) {

NSString *uniqueID;

if (sortType == kFIRST) {

uniqueID = [NSString stringWithFormat:@"%@ %@", f.fullName, f.myID];

}else if (sortType == kLAST) {

uniqueID = [NSString stringWithFormat:@"%@ %@", f.fullName_rev, f.myID];

}else if (sortType == kCITY) {

uniqueID = [NSString stringWithFormat:@"%@ %@", f.cityName, f.myID];

}

//add to dictionary and array 

[fullNames addObject:uniqueID];

[friendDictionary setObject:f forKey:uniqueID];

[uniqueID release];

}

//Sort the all string array (like magic!)

NSArray *sortedFriends = [fullNames sortedArrayUsingSelector:@selector(compare:)];

[fullNames release];

BOOL found=NO;

sectionsDictionary = [[NSMutableDictionary alloc] init];

//Now create friend array based on ordered friends

for (NSString *s in sortedFriends) {

SocialFriend *f = (SocialFriend*)[friendDictionary objectForKey:s];

NSString *firstCharacter = [s substringToIndex:1];

if ([sectionsDictionary objectForKey:firstCharacter]!=nil)

found = YES;

else 

found = NO;

if (!found) {

[sectionsDictionary setObject:[[NSMutableArray alloc]init] forKey:firstCharacter];

}

[[sectionsDictionary objectForKey:firstCharacter] addObject:f];

[f release];

}

return [[sectionsDictionary allKeys] count];

You’ll notice:

NSString *uniqueID = [NSString stringWithFormat:@"%@ %@", f.fullName, f.myID];

is how I append the two strings. Note that with the implementation above I never have to remove the ID from the string, because I link the uniqueID to an actual friend object. Find the friend then call the friend’s “firstName” and “lastName” ivars when I want to display the name in the tableview.

If I did have to remove it though it would look like this:

NSString *name = [uniqueID stringByReplacingOccurrencesOfString:f.myID withString:@""];

Questions? Comments? Ways to improve what I did? Let me know!

Sorry about the wordpress formatting too, anyone know how to change this shit?

-Tim

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s