0

I am reading a bunch of NSNumber values from a plist, and the first line in the code I'm showing you has the value of 2 for stoneOne[@"Column"]. When I set a breakpoint for 'c' in the second line, and examine [c intValue], c is correctly int 2. However, when I analyze the value of NSInteger column, the value is 528839664. Any reason for this? Should I just not get the intValue of c? Is it necessary? (I thought for converting NSNumber values to readable NSInteger values you had to call that method).

    NSNumber* c = stoneOne[@"Column"];
    NSInteger column = [c intValue];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Frank
  • 107
  • 9
  • My first guess is that you're examining with the debugger and get unexpected result because you are looking "too early", i.e. the line of code hasn't been executed yet, are looking at something else (other maybe global) variable or its address, or fell the trap of getting odd results debugging an optimized build, which sometimes gives problems with concrete lines of code, as the order in which they execute might get shuffled. – Eiko May 06 '16 at 05:51
  • FYI - Don't mix types. If `column` is an `NSInteger`, use `[c integerValue]`. If you want to use `intValue` then change the type of `column` to `int`. – rmaddy May 06 '16 at 06:20

1 Answers1

1

Ok, Two things.

As Eiko Pointed out, you need to go to the next line, to ensure the assignment like NSInteger column = [c intValue]; is executed. Just go one step ahead, or put the breakpoint in the line below it.

Secondly, use NSInteger column = [c integerValue]; to optimally convert to NSInteger.

Kakshil Shah
  • 3,466
  • 1
  • 17
  • 31
  • intValue and integerValue are very similar, and often just the same. In any case, both are wide enough to capture "2" without problem. ;-) (And even if would matter, "528839664" would fit, too.). – Eiko May 06 '16 at 06:12
  • 1
    @Eiko The recommendation to use `integerValue` is good advice here since `column` is declared as an `NSInteger`. Of course it is not directly related to the immediate issue. It's just the right thing to do. Don't mix and match types needlessly. – rmaddy May 06 '16 at 06:22
  • @rmaddy Totally agree about not mixing types. Making column an 'int' might be just as valid, though. – Eiko May 06 '16 at 06:27