answered Feb 22 '12 at 22:34, Ben Zotto:
A string and an integer are fundamentally different data types, and
casting in Objective-C won't do a conversion for you in this case,
it'll just lie to the compiler about what's happening (so it compiles)
but at runtime it blows up.
You can embed an integer directly into a format string by using %d
instead of %@:
tempString = [NSString stringWithFormat:@"%@%d%@%d", part1,num1, part2, num2];
NSInteger is just a fancy name for a regular "int"
(number). An NSString is an object reference to a string object. Some
numeric types (int and floating point) can be sort of converted
between eachother directly in C like this, but these two aren't
inter-operable at all. It sounds like you might be coming from a more
permissive language? :)
"Keep in mind that @"%d" will only work on 32 bit. Once you start using NSInteger for compatibility if you ever compile for a 64 bit platform, you should use @"%ld" as your format specifier." by Marc Charbonneau
So, solution:
tempString = [NSString stringWithFormat:@"%@%ld%@%ld", part1, (long)num1, part2, (long)num2];
Source: String Programming Guide for Cocoa - String Format Specifiers (Requires iPhone developer registration)