Strong Fortress.com
Building Better Games on a Sure Foundation
  • Home
  • Blog
  • Web Games
    • ExoTrex (Dig-It Games!)
    • Loot Pursuit: Maya (Dig-It Games!)
  • iPhone
    • DnD 4e Character Viewer
    • Hot Potato Original
    • Thunder Clash
  • Board Games
    • Farlanthia: Lords of War
  • Table-Top
    • Avalon: Mists of Time
    • Laser Squid Nemesis!!!
Select Page ...

Dev Blog

NSString and Key-Value Coding

Stephen Furlani 2011-01-20 Games

Ugh, so on my way to making awesome Apps, I feel like I need to “upgrade” the basic libraries the iOS SDK comes with. Today was one of those instances.

I needed to upgrade their String class NSString so that it could replace keys in it’s string with values from a NSDictionary. The following is the very simple result using a Category.

@interface NSString (NSString_SFKeys)

- (NSString*) stringByReplacingOccurrencesOfKeysWithValues:(NSDictionary*)keyValuePairs;

@end


@implementation NSString (NSString_SFKeys)
- (NSString*) stringByReplacingOccurrencesOfKeysWithValues:(NSDictionary*)keyValuePairs
{

NSString *fStr = self;
if (keyValuePairs) {

for (NSString *key in [keyValuePairs allKeys]) {

NSString *value = [NSString stringWithFormat:@"%@",[keyValuePairs valueForKey:key]];
fStr = [fStr stringByReplacingOccurrencesOfString:key withString:value];

}

}
return fStr;

}

@end

And that’s that. Works great! Now I can do stuff like add story text like the following:

It was a dark and stormy night. {Player} looked out {pospronoun} window at a blackened sky.

And have it print:

It was a dark and stormy night. Jimmy looked out his window at a blackened sky.

Yay for smartness!

← There be Dragons…
More Progress! →
Stephen Furlani

  • Recent Posts

    • RIFTS “Hand to Hand” Combat… and ranged attacks.
    • Continued RIFTS Complexity Discussion
    • RIFTS Discussion on Complexity
    • On the App Store!
    • Serious Progress
  • Archives

    • June 2012
    • March 2012
    • February 2012
    • October 2011
    • August 2011
    • July 2011
    • May 2011
    • March 2011
    • February 2011
    • January 2011
    • November 2010
    • September 2010
  • Categories

    • Games
    • Uncategorized
  • All content not © by Dig-It Games! is Copyright © 2012 - 2018 Stephen Furlani. All rights reserved.