code_burgar
code_burgar

Reputation: 12323

Obj-C crashes on substringWithRange message

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)];

Upvotes: 3

Views: 6721

Answers (3)

Amagrammer
Amagrammer

Reputation: 6373

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.

Upvotes: 6

Nick Moore
Nick Moore

Reputation: 15857

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.

Upvotes: 6

Ben Gottlieb
Ben Gottlieb

Reputation: 85542

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

Upvotes: 3

Related Questions