views:

621

answers:

3

The following code is making my app crash at line 3 without an error I would recognize or know how to deal with. Any ideas as to what I am doing wrong?

NSInteger *match = [str1 intValue] + [str2 intValue];
NSString *strrep = [NSString stringWithFormat:@"%i", match];
label.text = [strrep substringWithRange:NSMakeRange(1,3)];
+6  A: 

You want NSInteger match. No pointer. (NSInteger is not a class, it is just a typedef for int or long depending on your compilation target.)

Although, bizarrely, you code will probably still actually work like this, since the pointer itself will act in place of the int.

invariant
+3  A: 

If your string is less than (1 + 3) 4 characters long, this will crash.

Ben Gottlieb
Specifically, `substringWithRange:` will raise a `NSRangeException` in that case.
invariant
Thanks, but it crashes when strrep string is longer than 4 chars too :(
code_burgar
+5  A: 

I suggest you break line 3 into two lines, in order to isolate the issue.

NSString *result = [strrep substringWithRange:NSMakeRange(1,3)];
label.text = result;

If I had to guess, I'd say label has probably been released somewhere, and you are trying to assign to a bad location.

Amagrammer
So that fixed your bug?Chaining methods together is great for saving space and for making code more readable, but decomposing into multiple steps can be a bit help in debugging. It's almost always the first thing I do.
Amagrammer