0

I am trying to make a list of years based on their decade.

I have this forLoop:

for (int i = 0; i <= 9; i++) {

        NSString *thisYear = [NSString stringWithFormat: @"%ld", thisDecade];
        //NSLog (@"(long)selectedDecade++ %ld", (long)selectedDecade++ );
        thisDecade = thisDecade ++;
        NSLog (@"thisDecade %ld", thisDecade);
}

I use the result in a button label. Logging the output shows a difference between the label text and the loop results and I have a sore neck trying to figure out why.. Is it something in the loop? thanks

Logs:

[AppDelegate setYears]
selectedDecade: 1970
thisDecade 1971

 RightSideBar aTitle is 1970
thisDecade 1972


 RightSideBar aTitle is 1971
 thisDecade 1973

 RightSideBar aTitle is 1972
 thisDecade 1974

 RightSideBar aTitle is 1973
 thisDecade 1975

UPDATE - I appreciate all the help, but I remain stumped. I'm attaching a screenshot of the logs, and the code that produces them.. Thanks again..

-(void)addButtonWithTitle:(NSString*)aTitle image:(NSImage*)image alternateImage:

(NSImage*)alternateImage
{
    //NSLog(@"%s", __FUNCTION__);
    NSButtonCell * cell = [self addButton];
    [cell setTitle:aTitle];
    NSLog(@"RightSideBar aTitle is %@", aTitle); << see the logs vs the screenshot
    [cell setImage:image];
    [cell setAlternateImage:alternateImage];
}

logs:

enter image description here

enter image description here

UPDATE2: It turns out that I subclassed the custom cell incorrectly, and as a result, called it too many times..I will mark as complete as you all gave me great ideas to work with.I wish we could award multiple points..

ICL1901
  • 7,632
  • 14
  • 90
  • 138
  • 3
    `thisDecade = thisDecade ++;` you just need `thisDecade++;` – Mike Weller Jul 22 '13 at 15:01
  • 3
    Also, `thisDecade = thisDecade++` is undefined behavior, as there is no sequence point between the assignment and increment. See [this discussion](http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points) on why. – Richard J. Ross III Jul 22 '13 at 15:19

2 Answers2

5

Your problem is probably in the line

thisDecade = thisDecade ++;

You should keep in mind that the in- and decrement operators (++, --) already store the new value in the variable. So this would be ok:

++thisDecade;

What you are trying to do is undefined. Take look at this, starting at slice 142: http://www.slideshare.net/olvemaudal/deep-c

JDS
  • 1,173
  • 12
  • 26
  • Thanks to you both. The slideshare presentation is great. I need to work through it. However, neither ++thisDecade or thisDecade++ results in different logs than in my question. Is there somewhere else I should look? – ICL1901 Jul 22 '13 at 15:16
2

All of the strings created for thisYear (presumably what you are using to title your buttons) are going to be off by one year because you are incrementing the thisDecade variable after using it to format your strings. I believe you should either move the incrementing before the NSString formatting or move it after your NSLog statement. Also, you only need to write thisDecade++; to increment the variable.

Patrick Goley
  • 5,397
  • 22
  • 42
  • Thanks Patrick - that's very helpful. I'm off to see what I can make of your answer, but I sense you are correct. – ICL1901 Jul 22 '13 at 15:31