15

I'm trying to make one string out of several different parts. This below is what i have right now. I don't get any errors in my code but as soon as i run this and do the event that triggers it i get EXC_BAD_ACCESS. Is there another way of casting this NSInteger into a NSString?

NSString *part1, *part2, *tempString;

NSInteger num1;
NSInteger num2;

part1=@"some";
part2=@"text";    

tempString = [NSString stringWithFormat:@"%@%@%@%@", 
              part1,
              (NSString *)num1, 
              part2, 
              (NSString *)num2];
Alex Cio
  • 6,014
  • 5
  • 44
  • 74

7 Answers7

22

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? :)

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
18

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)

Community
  • 1
  • 1
saintech
  • 357
  • 6
  • 12
  • Almost correct. You don't need to explicitly cast the numbers to long, if you simply use the `%ld` formatters. – mcandre Nov 03 '13 at 20:04
  • 4
    With Xcode 5.1.1, a warning is generated if NSInteger values aren't cast to (long) when using %ld formatters. – Alex Shaffer Jun 25 '14 at 11:52
1

You have to use %d for integer and not %@.

So, [NSString stringWithFormat:@"%@%d%@%d", part1,num1, part2, num2]; is the correct code to format your string.

Hope it helps.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
0
NSString *part1, *part2, *tempString;

NSInteger num1;
NSInteger num2;

part1=@"some";
part2=@"text";

tempString = [NSString stringWithFormat:@"%@%d%@%d", part1,num1, part2, num2];
NSLog(@"%@",tempString);
aalesano
  • 357
  • 2
  • 13
0
NSString* tempString = [NSString stringWithFormat:@"%@%d%d", part1, num1, num2];
Dmitry S.
  • 8,373
  • 2
  • 39
  • 49
0

try the following:

 tempString = [NSString stringWithFormat:@"%@%d%@%d", part1,(NSString *)num1, part2, (NSString *)num2];
atbebtg
  • 4,023
  • 9
  • 37
  • 49
0

NSIntegers are actual integers - not NSObject-derived objects. Try:

tempString = [NSString stringWithFormat:@"%@%d%@%d", part1, num1, part2, num2];

The cast trying to force NSInteger into NSString* is also dangerous, and probably the actual source of your crash. The compiler is letting you get away with it because it's trusting you know what you're doing with that kind of casting statement.

fbrereto
  • 35,429
  • 19
  • 126
  • 178