Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nickculbertson

macrumors regular
Original poster
Nov 19, 2010
226
0
Nashville, TN
With the following code I am trying to fire an action to add +1 to an integer in a UILabel. When the action is fired the number adds +1 fine. The problem I'm facing, is telling the label which number is displayed. From my console I am getting "2" every time I fire the action, even when the value of the label is "4" (in which case I'd like the console to read "1"). Any idea what I've done wrong?

Code:
- (IBAction) xplus1{	
		Count1 = Count1 + .5;
		NSString *numValue = [[NSString alloc] initWithFormat:@"%d",  Count1++];
		NSLog(numValue);	
	
	if (l1.text == @"4")
	{	NSLog(@"1");
		l1.text = @"A";
		[numValue release];
	}else {
		NSLog(@"2");
                l1.text = @"B";
		[numValue release];
	}

	
	
}
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
First, is Count1 an int?

Second, if it is, why are you adding .5 to it?

Third, == is not the proper operator to use to check equality with an NSString. It has an instance method built specifically for that purpose.
 

nickculbertson

macrumors regular
Original poster
Nov 19, 2010
226
0
Nashville, TN
First, is Count1 an int?

Second, if it is, why are you adding .5 to it?

Third, == is not the proper operator to use to check equality with an NSString. It has an instance method built specifically for that purpose.

Count1 is an int. I used .5 instead of 1 because when I used 1 it returned 0,2,4,6,8...

Thank You. I now see that I need to use isEqualToString:

I'm slightly perplexed but the following code seems to work. I would think it shouldn't work because I never pointed l1 to numValue.

Code:
- (IBAction) xplus1{

		Count1 = Count1 + .5;
		NSString *numValue = [[NSString alloc] initWithFormat:@"%d",  Count1++];
		NSLog(numValue);	
	
	if ([l1.text=@"4" isEqualToString:numValue])
	{	NSLog(@"1");
		l1.text = @"A";
		[numValue release];
	}else if ([l1.text=@"6" isEqualToString:numValue])
	{	NSLog(@"1");
		l1.text = @"C";
		[numValue release];
	}else {
		l1.text = @"B";
		NSLog(@"none");
		[numValue release];
	}

	
	
}
 

chown33

Moderator
Staff member
Aug 9, 2009
10,740
8,416
A sea of green
Count1 is an int. I used .5 instead of 1 because when I used 1 it returned 0,2,4,6,8...

It's doing that because of the Count++ in your initWithFormat message.

You should review the incrementing operators ++ and --, and the difference between a suffixed operator (such as Count++) and a prefixed operator (such as ++Count).

Code:
	if ([l1.text[COLOR="Red"]=@"4"[/COLOR] isEqualToString:numValue])
	{	NSLog(@"1");
		l1.text = @"A";
		[numValue release];
	}else if ([l1.text[COLOR="red"]=@"6"[/COLOR] isEqualToString:numValue])
I doubt that either of the red-hilited expressions are doing what you intend to happen.

Frankly, I'm not sure what you intend to happen, even though I read your earlier descriptions several times. Please give a concrete example of what should happen. Example of a concrete example:
1. The label says "3".
2. Click the button.
3. The label changes to "4".
4. Click the button again.
5. The label changes to "Cabbages".


EDIT:
Here's another example.

What it does:
1. Adds 1 to Count1.
2. Converts Count1 to a string.
3. Logs the string.
4. Assigns the string to label l1's text property.

Each step above has a simple corresponding statement in the code. I recommend writing code this way to make it clear that the code and the description actually correspond to one another.

Code:
- (IBAction) xplus1
{
  Count1 = Count1 + 1;  // one way to add 1
  NSString * numValue = [NSString stringWithFormat:@"%d", Count1];
  NSLog( numValue );
  l1.text = numValue;
}

What it doesn't do:
A. It doesn't check for any particular value of Count1.
B. It doesn't show "A" or "B" or "Cabbages" when a particular value occurs.


One way to add 1 to Count1 is this:
Code:
  Count1 = Count1 + 1;
Another way is this:
Code:
  Count1 += 1;
These two ways are equivalent.

One way to add 1 that uses ++ is this:
Code:
  ++Count1;
And the other way that uses ++ is this:
Code:
  Count1++;
These two ways are equivalent in this case, but that isn't always true.

There is a significant difference when ++ is used in an expression like stringWithFormat:. The former increments Count1 then uses the resulting value. The latter uses the value of Count1 before it's incremented, then increments the variable.
 
Last edited:

nickculbertson

macrumors regular
Original poster
Nov 19, 2010
226
0
Nashville, TN
You should review the incrementing operators ++ and --, and the difference between a suffixed operator (such as Count++) and a prefixed operator (such as ++Count).


I doubt that either of the red-hilited expressions are doing what you intend to happen.

1. Thanks, I better understand the difference in ++... and ...++ now.

2. They are actually doing what I want them too but I can see how I've made it look confusing.
I'm trying to fire the action and have the label read B, however on the fourth time the action is called it will read A, and on the 6th time C.

I'm just testing out the principle. Ultimately, I will have the the label say "A" and it will perform another IBAction.

Another question... Where should I look to find a way to have the max value of numValue be equal to "12" and the anything greater would be equal to "1"?


Thanks,
Nick
Thanks,
Nick
 

chown33

Moderator
Staff member
Aug 9, 2009
10,740
8,416
A sea of green
Another question... Where should I look to find a way to have the max value of numValue be equal to "12" and the anything greater would be equal to "1"?

The numValue string is nothing more than a secondary string representation of the integer Count1. Why use a secondary value when you can use the actual primary value, Count1 itself?

I recommend looking at how to compare integers to one another, and then perform a particular action based on the comparison. Simple example (not compiled or tested):
Code:
if ( Count1 > 12 )
  Count1 = 1;
If you don't know about numeric comparisons, you need to stop coding and go review the basics.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.