Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 30, 2011, 01:41 PM   #1
nickculbertson
macrumors regular
 
Join Date: Nov 2010
Location: Nashville, TN
UILabel number recognition?

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

	
	
}
__________________
My App Blog --->
nickculbertson is offline   0 Reply With Quote
Old Jan 30, 2011, 02:11 PM   #2
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: 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.
__________________
dejo is offline   0 Reply With Quote
Old Jan 30, 2011, 04:50 PM   #3
nickculbertson
Thread Starter
macrumors regular
 
Join Date: Nov 2010
Location: Nashville, TN
Quote:
Originally Posted by dejo View Post
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];
	}

	
	
}
__________________
My App Blog --->
nickculbertson is offline   0 Reply With Quote
Old Jan 30, 2011, 05:18 PM   #4
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by nickculbertson View Post
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=@"4" isEqualToString:numValue])
	{	NSLog(@"1");
		l1.text = @"A";
		[numValue release];
	}else if ([l1.text=@"6" 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 by chown33; Jan 30, 2011 at 06:22 PM.
chown33 is offline   0 Reply With Quote
Old Jan 30, 2011, 06:32 PM   #5
nickculbertson
Thread Starter
macrumors regular
 
Join Date: Nov 2010
Location: Nashville, TN
Quote:
Originally Posted by chown33 View Post
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
__________________
My App Blog --->
nickculbertson is offline   0 Reply With Quote
Old Jan 30, 2011, 07:26 PM   #6
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by nickculbertson View Post
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.
chown33 is offline   0 Reply With Quote
Old Jan 30, 2011, 08:08 PM   #7
nickculbertson
Thread Starter
macrumors regular
 
Join Date: Nov 2010
Location: Nashville, TN
Quote:
Originally Posted by chown33 View Post
Code:
if ( Count1 > 12 )
  Count1 = 1;
Worked Like a Charm! Thanks!

Quote:
Originally Posted by chown33 View Post
If you don't know about numeric comparisons, you need to stop coding and go review the basics.
ouch... the truth hurts. Prior to now, I've not needed integer comparisons. I've certainly learned a lot about it today.

Thanks,
Nick
__________________
My App Blog --->

Last edited by nickculbertson; Jan 30, 2011 at 08:43 PM.
nickculbertson is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Writing value to UILabel Kapthehat iPhone/iPad Programming 6 Jul 19, 2013 08:32 AM
Can't move a IBOutlet UILabel position hontrok iPhone/iPad Programming 6 Nov 14, 2012 06:41 AM
UILabel Help lukin iPhone/iPad Programming 3 Sep 18, 2012 07:36 PM
sizeToFit() for UILabel is moving left. Narendar Singh iPhone/iPad Programming 10 Jul 11, 2012 10:11 AM
Stupid UILabel question mngoose iPhone/iPad Programming 7 Jun 11, 2012 08:58 PM

Forum Jump

All times are GMT -5. The time now is 07:57 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC