Sunday, November 28, 2010

Ruby Scripts to Get Keys As Array for Objective-C

It's a common situation to have a line like this

@synthesize control, command, channel, data1, min, max;

which you want to make into one of these

NSArray *arrayOfKeys = [NSArray arrayWithObjects:@"control", @"command", @"channel", @"data1", @"min", @"max", nil];

While I could look for the Objective-C way to do it, another way is to cut my losses and do it with Ruby. Just fire up irb and paste this in

keys = "control, command, channel, data1, min, max"
keys = keys.split(", ")
text = "NSArray *arrayOfKeys = [NSArray arrayWithObjects:"
keys.each {|key| text << %Q!@"#{key}", ! }
text << "nil];"
puts text 


No comments: