1

I am very new to Objective-c and probably really easy to solve but couldnt find an answer anywhere....

I am trying to add +1 to a variable every time the user clicks on the button but instead of adding +1 it adds +4

- (IBAction)addNewSet:(UIButton *)sender {
    NSLog(@"%i",_sliderTag);
    _sliderTag += 1;
   NSLog(@"ADD NEW    %i",_sliderTag);
}

_sliderTag is already an NSInteger:

@property (nonatomic,assign) NSInteger* sliderTag;

The first NSLog prints 0 and the 2nd after the add is performed prints 4. Could anyone explain why? It is meant to print 0 the first one, as the point of this variable is to be a counter for setting tags.

jszumski
  • 7,430
  • 11
  • 40
  • 53
Jonathan Thurft
  • 4,087
  • 7
  • 47
  • 78
  • Because you declared sliderTag to be a pointer rather than an int. Didn't even need to look at your property declaration to tell that. – Hot Licks Apr 23 '13 at 21:03
  • As the others said in their answers, you are using a pointer to an `NSInteger`. `NSInteger` isn't a class (as it indicates) but a typedef to something very similar to int. – HAS Apr 23 '13 at 21:03
  • @HAS - It's not whether it's a class or not, it's whether it's a pointer or not. When you increment a pointer in C you increment by the size of the element pointed to. One should not use a pointer where an int is intended. – Hot Licks Apr 23 '13 at 21:05
  • @HotLicks Your are right, but usually when you (as a beginner) see `NSSomething` you think "Oh it's a class so create a pointer as usual". That's what he did, I think. But in that case he doesn't want a pointer. – HAS Apr 23 '13 at 21:07
  • Even if it were an NSNumber (which *is* a class), you wouldn't increment it that way. Any time you're incrementing a pointer you're treading on thin ice. – Hot Licks Apr 23 '13 at 21:13
  • @HotLicks Sorry, I talked about the `@property` declaration, not the incrementing – HAS Apr 23 '13 at 21:18

2 Answers2

6

Sounds like _sliderTag is a pointer to a type whose size is 4 bytes. Adding 1 to a pointer increments it by the size of the type it points to. Here are two examples that illustrate the difference:

NSInteger foo = 0;
foo += 1;
NSLog(@"result: foo = %d", foo);    // result: foo = 1

NSInteger *bar = 0;                 // note the '*'
bar += 1;
NSLog(@"result: bar = %d", bar);    // result: bar = 4
Caleb
  • 124,013
  • 19
  • 183
  • 272
  • 2
    It is because you declared it as a pointer, `NSInteger *`, remove the star. `sizeof(*_sliderTag)` happens to be 4 on your system and increasing the pointer by 1 results in an address of 4 being printed. – Joe Apr 23 '13 at 21:01
  • 2
    @JonathanThurft - sliderTag is *not* an NSInteger, it's a pointer to NSInteger. Entirely different thing. (And a difference you need to know before you go ANY farther programming in Objective-C.) – Hot Licks Apr 23 '13 at 21:06
1

first, make sure _sliderTag is an int or Integer or int and not Integer* or int*, second, dont print it with %i, but with %d

Dima
  • 8,586
  • 4
  • 28
  • 57
  • Why not print with %i? When you print they do the same, see this SO question: http://stackoverflow.com/questions/1893490/difference-between-format-specifiers-i-and-d-in-printf – HAS Apr 24 '13 at 07:01
  • just something i used to so i wrote it to be sure its like on my computer – Dima Apr 24 '13 at 07:07