A Lent Miracle

This post is going to be a bit off topic, but I had to write it down if only for my record of bizarre things…

During this time of year, between Ash Wednesday and Easter, most forms of christians (I think) forego a bad habit or adopt a good one. I, not being very religious to begin with, decided I should stop purchasing alcohol. I realize this is a bit of a cop out, and there are plenty work-a-rounds for me to get alcohol. A popular one is where I buy a friend food and they buy me a drink. The trouble comes into play when there is no one around but myself.

This is where, I dare say, a lent miracle occurred. One faithful Wednesday night the room mates were out of town, and I really felt like grabbing a drink. (I’m not an alcoholic, I have Thursdays off) I started to pre-game or as we now call it “getting drip” at my place. Taking shots of some gin my friend had left in the freezer. I was kinda bored though drinking alone, so I decided “screw it, I’m going to the bar, I’m going to break lent!!”. Note: I really didn’t care about breaking lent, but spending money on alcohol at a bar is not something I need to do either.

So I arrive at the Ruby Room, a fine establishment in downtown Oakland. Nice atmosphere, great drinks, I heartily recommend going there. Anyway, I take a seat at the bar and order a drink called the “Ruby”. I figured I’d splurge a little, after all I am breaking a rule I set for myself. The bartender goes upstairs to get some pint glasses, and fixes me my drink. I pull out my credit card and just as I’m about to hand it to her, she says “this ones on me.” WHAT!? you gotta be joking! Her reasoning was that she took a while to make my drink but I’ve waited much longer, I’ve had bartenders mess up and remake my drink, and it’s not on the house. Also, that has yet to happen to me- bartenders never buy me a drink. Sure maybe after a couple rounds I might get a free shot, usually when I’m with a group of people, but never the first drink, and an expensive mixed drink at that. Thus, say what you will, I say miracle… I was so pleased and sufficiently drunk that I didn’t bother purchasing a drink for the remainder of the night. Seriously though, what are the odds?

Advertisements

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


Hello world!

I’ve decided not to change the title of this post. Given the subject matter of my blog I found it fitting.* I guess this is a good place to start some sort of introduction. Hi. Now that that’s done I can begin talking about what to expect on this blog. I think this will primarily serve as a development diary, although I’ll include other things too. Maybe some strange observations, or something to break away from topics unsuitable for non-developers, to make this less monotonous.

Stuff I’m Developing:

  • DigitSync iOS facebook contact synchronizer.
  • Draconem (name pending) iOS arcade style game.
  • Hobbyfan App for iOS (supposedly I’m making an app for where I work)

Other Stuff I Do:

  • Play Games (Currently playing MvC 3)
  • Go Out (Oakland nightlife scene? maybe)
  • Workout (I like to keep in shape, routine, diet… eh I’ll think of something)

Future Topics:

  • XNA X-Box Dev** (I literally just downloaded XNA 4.0 with Visual C# Express Edition)

*a “Hello World” is typically the first program written by computer programmers learning a new programming language or framework. teehee.

** I’ll probably abbreviate the word develop(ment) with dev, and developer with devver. Just a heads up.