Late Update

Is kind of an oxymoron but that’s ok because oxymorons tickle my brain in a good way. My favorite is definitely “random order”.

Anyway, I wrote some code that helps grab a section of a string (substring) from a larger string, without knowing what’s between the two strings. So it’s kind of useful for scanning/parsing a chunk of data. (look below for application)

The functions:

-(NSInteger)getScanLocation:(NSString *)content toString:(NSString *)lookup {

NSScanner *scanner = [NSScanner scannerWithString:content];

[scanner scanUpToString:lookup intoString:nil];

return [scanner scanLocation];

}

-(NSString *)grabInfo:(NSString *)content fromString:(NSString *)lookup {

NSInteger startScan = [self getScanLocation:content toString:lookup];

NSInteger lengthOfInfo = [content length];

NSRange start_range = NSMakeRange(startScan,(lengthOfInfo - startScan));

NSString *info = [content substringWithRange:start_range];

return info;

}

-(NSString *)getSection:(NSString *)content fromString:(NSString *)startString toString:(NSString *)endString {

NSString *first = [self grabInfo:content fromString:startString];

NSInteger endScan = [self getScanLocation:first toString:endString];

NSRange end_range = NSMakeRange(0, endScan);

NSString *final = [first substringWithRange:end_range];

return final;
}

As you could probably tell it’s not fully optimized or anything but for now it works. The application would look something like…

You have some NSString “data” e.g.

<example data>blahblah<data>/\/\/\120934u534814[j 109]<more crap><random order><chaos control>120938103453410

<more data><this is a lot of data we’re talking about here><what><yes><no>10348304803410922<oh man><test>

Now say I want what’s in bold, I would type:


NSString *data_section = [self getSection:data fromString:@"<more data>" toString:@"<yes>"];

Questions? Thoughts? Comments? Concerns?


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