Weird behavior with UILabel and Text propert - iphone

Im trying to do something that I think is super simple.
i have 3 integers - prevgues1 , 2 and 3
and i have 3 UILabels prevguess1, 2 and 3
the ints have 1 less s.
When I set the text of the label so
prevguess1.text = [NSString stringWithFormat:#"%d", prevguess1]
prevguess2.text = [NSString stringWithFormat:#"%d", prevguess2];
prevguess3.text = [NSString stringWithFormat:#"%d", prevguess3];
the label just set to a number like 89324 or something like that.
I just don't know what my problem is.
Any ideas would be helpful
Cheers
Sam
Note:
I have tried setting the text simply to a string - and have had luck.
but when i set it to a integer, which start as 0 value, (in viewdidload) the weirdness happens

prevguess1.text = [NSString stringWithFormat:#"%d", prevguess1];
Here you treat prevguess1 as a label and as an integer - probably there's a typo somewhere?
Even if it's not a matter of your problems I think you should consider changing the way you name your variables a bit to avoid possible confusion.

Related

How to Multiply Two textfield values Without converting the text into integer or Number

I have two text fields and user entered the values. I can get the values of textFields like below
NSString *number1 = firstTextField.text;
NSString *number2 = secondTextField.text;
I want to multiply number1 and number2 without converting them into integer or number.I am doing like below
NSExpression *expression = [NSExpression expressionWithFormat:[NSString stringWithFormat:#"%#*%#",number1,number2]];NSLog(#"Multiplication result is----%#",[expression expressionValueWithObject:nil context:nil]);
I don't know if it is correct or not. If it is not correct please give me the suggestions how it can be possible.
By using NSExpression is one way to Multiply/Add/Subtract two number strings without converting them into integer or number.
NSExpression *expression = [NSExpression expressionWithFormat:expressionFormat];
You are using here expressionFormat as [NSString stringWithFormat:#"%#*%#",number1,number2];.
If you want to add or subtract number1 and number2 then replace * with + or -. In this way you have to proceed.
If the question was in an interview.
The interviewers were probably expecting you to write a method to go through both arrays and multiply the characters (converting one by one to integers) or (also identifying the represented character to know the equivalent integer number).
Searching on google there are some examples in different languages.
JAVA
http://csjobinterview.wordpress.com/2012/04/05/string-multiplication/
string multiplication
C++
Multiplying two number arrays
It is a common question in interviews.
I simply used textfields... and used the values that the user input into those textfields... here is one calculation example that has worked for me... in this example the user is trying to find the volume of beachstone they would need to order... the formula is pretty straight forward. Make sure you use the math brackets () to distinguish order of operations...
-(IBAction)calculate_beachstone:(id)sender {
float floatanswer =
([area1.text floatValue])+([area2.text floatValue])+([area3.text floatValue])+([area4.text floatValue])+([area5.text floatValue])+([area6.text floatValue])+([area7.text floatValue])+([area8.text floatValue])+([area9.text floatValue])+([area10.text floatValue]))
*([beachstone_depth.text floatValue])/12)/27);
NSString *stringRectResult=[[NSString alloc]
initWithFormat:#"%.1f",floatanswer];
answer_beachstone.text=stringRectResult;
}
I am adding the text found in the textfield (which user can only use numbers for input - custom keyboard)... in this example I have up to 10 fields which can be added together... then I use the * (multiply) to apply a depth in this example, and convert it back to a text string so I can display the result somewhere else...
In fact, if you write this part answer_beachstone to NSUserDefaults, you can use the result anywhere in different controllers, by calling it back.
If you're interested, here is how I did that...
-(IBAction)save_answer_beachstone:(id)sender {
save_answer_beachstone = [[NSString alloc] initWithString:answer_beachstone.text];
[answer_beachstone setText:save_answer_beachstone];
NSUserDefaults *save = [NSUserDefaults standardUserDefaults];
[save setObject:save_answer_beachstone forKey:#"save_answer_beachstone"];
}
Then I can use the resulting "answer" in any controller inside viewDidLoad...
[answer_beachstone setText:[[NSUserDefaults standardUserDefaults] objectForKey:#"save_answer_beachstone"]];
I know the question was asked 4 years ago, but this formula syntax works for me in a number of ways, in various apps...
You can't multiply two strings. It's not possible.
For this you have to convert it into Integer or int using NSNumber or using:
[secondTextField.text intValue].
NSInteger number = [firstTextField.text integerValue]*[secondTextField.text integerValue];

Issue formatting string in xcode iphone

I'm a noob to iphone development (2nd day using xcode) and I am having trouble making a dynamic text for a label. My string:
dealerspremiumLabel.text= [NSString stringWithFormat:#"%d%% (%#%d)", premiumPercentage, symbol, premiumCurrency];
is returning as "0% ((null)88988)" instead of "0% ($00.00)". I can't figure out why this is happening because in ViewDidLoad I set my variables as:
premiumPercentage=0;
premiumCurrency= 00.00;
symbol=#"$";
dealerspremiumLabel.text= [NSString stringWithFormat:#"%d%% (%#%d)", premiumPercentage, symbol, premiumCurrency];
My label outlet is properly set and my variables are setup properly as well. I also have checked my Log and the values for these particular variables are set correctly before the above code. Any help figuring this out is greatly appreciated.
Use this way,
currency is float you are using %d instead of %f
dealerspremiumLabel.text= [NSString stringWithFormat:#"%d%% (%#%.2f)", premiumPercentage, symbol, premiumCurrency];
EDIT:
As your symbol is giving null, you have missed to alloc+init.
Or you can directly use :
dealerspremiumLabel.text= [NSString stringWithFormat:#"%d%% ($%.2f)", premiumPercentage, premiumCurrency];
NSInteger premiumPercentage=0;
CGFloat premiumCurrency= 00.00;
NSString *symbol1=#"$";
symbol.text= [NSString stringWithFormat:#"%d%% (%#%f)", premiumPercentage,symbol1,premiumCurrency];
NSLog(#"text=%#",symbol.text);
I used this one and its working perfectly fine in my case and its also showing correct value in UILabel

How can you read 0's at the start of an integer or string? To then compare them

I am trying to code a textField so that is someone put in the number 1 only it will show up 10 and if the put in the number 01 it will show up 01.
The problem I have is that when I code an if statement it can't seem to read the 0 at the start of the number so I end up with 010 or just 1.
I have tried to use integers but get the same result and have tried == and compare for strings but nothing.
NSString *millieString = [[NSString alloc]initWithFormat:#"%#",timeFieldMillie.text];
if([millieString compare:#"01"]){
timeFieldMillie.text = [[NSString alloc]initWithString:millieString];
}
else if ([millieString compare:#"1"]){
timeFieldMillie.text = [[NSString alloc]initWithFormat:#"%#0", millieString];
}
This is probably because the first 0 is not being read but I can't seem to fix it.
I am trying to make a time converter and this is for the milliseconds so I need it to be 2 digits long.
Thanks
Use -isEqualToString: instead
You're leaking memory
The string is reset without the need to do it in your first if
Here's a simpler version:
if ([timeFieldMillie.text isEqualToString:#"1"])
timeFieldMillie.text = [NSString stringWithString:#"01"];
Or simply:
if ([timeFieldMillie.text isEqualToString:#"1"])
timeFieldMillie.text = #"01";
Have you try if ([millieString isEqualToString:#"01"])?
NSString compare return NSComparisonResult, try use isEqualToString

Set objectAtIndex to be the integer in an NSString?

I've got this:
if ([[currentPlayer rankicon] isEqualToString:#"0"]) {
self.casteImage.image = [casteImages objectAtIndex:0];
}
Which works. However, I have some 16 images to assign, and since the NSString and the index id are the same, I'd like to find a more efficient way to do this. I've also tried:
NSInteger i = [[currentPlayer rankicon] integerValue];
self.casteImage.image = [casteImages objectAtIndex:[i]];
But that doesn't work. Could be a bug elsewhere in my code(or some syntax error in the above), of course, but was wondering if I'm anywhere near the right track.
Remove [] around iin the second line.

objective-c code to right pad a NSString?

Can someone give a code example of how to right pad an NSString in objective-c please?
For example want these strings:
Testing 123 Long String
Hello World
Short
if right padded to a column width of say 12: and then a sting "XXX" is added to the end of each, it would give:
Testing 123 xxx
Hello World xxx
Short xxx
That is a 2nd column would like up.
Adam is on the right track, but not quite there. You do want to use +stringWithFormat:, but not quite as he suggested. If you want to pad "someString" to (say) a minimum of 12 characters, you'd use a width specifier as part of the format. Since you want the result to be left-justified, you need to precede the width specifier with a minus:
NSString *padded = [NSString stringWithFormat:#"%-12#", someString];
Or, if you wanted the result to be exactly 12 characters, you can use both minimum and maximum width specifiers:
NSString *padded = [NSString stringWithFormat:#"%-12.12#", someString];
2nd column of what would line up?
Given that you are on iOS, using HTML or a table view would be far more straightforward than trying to line up characters with spaces. Beyond being programmatically more elegant, it will look better, be more resilient to input data changes over time, and render a user experience more in line with expectations for the platform.
If you really want to use spaces, then you are going to have to limit your UI to a monospace font (ugly for readability purposes outside of specific contexts, like source code) and international characters are going to be a bit of a pain.
From there, it would be a matter of getting the string length (keeping in mind that "length" does not necessarily mean "# of characters" in some languages), doing a bit of math, using substringWithRange: and appending spaces to the result.
Unfortunately, Objective-C does not allow format specifiers for %#. A work-around for padding is the following:
NSString *padded = [NSString stringWithFormat:#"%#%*s", someString, 12-someString.length, ""];
which will pad the string to the right with spaces up to a field length of 12 characters.
%-# does not work, but %-s works
NSString *x = [NSString stringWithFormat:#"%-3#", #"a" ];
NSString *y = [NSString stringWithFormat:#"%-3#", #"abcd" ];
NSString *z = [NSString stringWithFormat:#"%-3# %#", #"a", #"bc" ];
NSString *zz = [NSString stringWithFormat:#"%-3s %#", "a", #"bc" ];
NSLog(#"[%#][%#][%#][%#].......", x,y,z,zz);
output:
[a][abcd][a bc][a bc].......
Try below. its working for me.
NSString *someString = #"1234";
NSString *padded = [someString stringByPaddingToLength: 16 withString: #"x" startingAtIndex:0];
NSLog(#"%#", someString);
NSLog(#"%#", padded);
First up, you're doing this a bad way. Please use separate labels for your two columns and then you will also be able to use proportional fonts. The way you're going about it you should be looking for an iPhone curses library.
If you really have to do it this way just use stringWithFormat:, like:
NSString *secondColumnString = #"xxx";
NSString *spacedOutString = [NSString stringWithFormat:#"testingColOne %#", secondColumnString];
NSString *spacedOutString = [NSString stringWithFormat:#"testingAgain %#", secondColumnString];