I’ve been learning Objective-C, the native language of iPhone development. Overall it seems like a reasonable language, albeit one that’s pretty tightly integrated with Xcode. (The large number of lengthy constants used in Cocoa/Objective-C programming would make me reluctant to develop in this framework/language outside of an IDE.) However, it does seem a little … verbose. In the course of writing a small piece of test code, I was struck in particular by Objective-C’s poor string handling.
The Problem
I wanted to generate the following block of text programmatically. It’s an 80×24 block of alphanumerics:
A1234567890123456789012345678901234567890123456789012345678901234567890123456789
B1234567890123456789012345678901234567890123456789012345678901234567890123456789
C1234567890123456789012345678901234567890123456789012345678901234567890123456789
D1234567890123456789012345678901234567890123456789012345678901234567890123456789
E1234567890123456789012345678901234567890123456789012345678901234567890123456789
F1234567890123456789012345678901234567890123456789012345678901234567890123456789
G1234567890123456789012345678901234567890123456789012345678901234567890123456789
H1234567890123456789012345678901234567890123456789012345678901234567890123456789
I1234567890123456789012345678901234567890123456789012345678901234567890123456789
J1234567890123456789012345678901234567890123456789012345678901234567890123456789
K1234567890123456789012345678901234567890123456789012345678901234567890123456789
L1234567890123456789012345678901234567890123456789012345678901234567890123456789
M1234567890123456789012345678901234567890123456789012345678901234567890123456789
N1234567890123456789012345678901234567890123456789012345678901234567890123456789
O1234567890123456789012345678901234567890123456789012345678901234567890123456789
P1234567890123456789012345678901234567890123456789012345678901234567890123456789
Q1234567890123456789012345678901234567890123456789012345678901234567890123456789
R1234567890123456789012345678901234567890123456789012345678901234567890123456789
S1234567890123456789012345678901234567890123456789012345678901234567890123456789
T1234567890123456789012345678901234567890123456789012345678901234567890123456789
U1234567890123456789012345678901234567890123456789012345678901234567890123456789
V1234567890123456789012345678901234567890123456789012345678901234567890123456789
W1234567890123456789012345678901234567890123456789012345678901234567890123456789
X1234567890123456789012345678901234567890123456789012345678901234567890123456789
Python
For purposes of comparison, here’s the code I’d used to print this block of text in Python:
import string
cols = string.digits[1:]+string.digits*7
print '\n'.join(r+cols for r in string.ascii_uppercase[:24])
Objective-C
Here’s the best that I was able to do in Objective-C:
NSString* rows = @"ABCDEFGHIJKLMNOPQRSTUVWX";
NSString* stub = @"0123456789";
NSString* cols = [@"123456789"
stringByAppendingFormat:@"%@%@%@%@%@%@%@",stub,stub,stub,stub,stub,stub,stub];
NSMutableArray* temp = [NSMutableArray arrayWithCapacity:rows.length];
for (int i = 0; i < rows.length; i++)
[temp
insertObject:[NSString stringWithFormat:@"%c%@",[rows characterAtIndex:i],cols]
atIndex:i];
console.text = [temp componentsJoinedByString:@"\n"];
(This code actually assigns the block of text to a UI element, but that's a minor difference.)
Uhg
I don't like the Objective-C version. It's much longer, and much uglier. I'm new to the language, so it's possible I'm missing something (please write if you have any suggestions) but this code is the result of a reasonable amount of Googling and experimentation.
I don't have much to add at this point, other than to remark that this makes me a sad panda.