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

art0071

macrumors newbie
Original poster
May 20, 2009
10
0
Hello,

In my first app, i need get a text from UITextField, convert and set into UitextField.

My code is:

int *valorcinq = (int) [campoHoraCinq text];
int *valorcem = (int) [campoHoraCem text];

valorcinq = valorcinq + valorcem; --> Error: invalid operands to binary +

:(

Tks for help-me
 
Why are you declaring valorcinq and valorcem as pointers?

I thought it was necessary to declare a variable with *.
For basic types like int, double, etc. need not be declared as pointer?

Sorry if the question is simple, I'm starting to look more objective-c now.

Thanks for Help
 
I thought it was necessary to declare a variable with *.
For basic types like int, double, etc. need not be declared as pointer
No, objects are declared with *. Basic types are not objects.

I think you might wanna step away from the real code for a bit and go review the basics of Objective-C, as well as its interaction with C.
 
No, objects are declared with *. Basic types are not objects.

I think you might wanna step away from the real code for a bit and go review the basics of Objective-C, as well as its interaction with C.

I'm watching the podcast and I'm even more in the beginning.

I have one more question:
When I do the command;

(int) valorcinq int = [campoHoraCinq text];
NSLog (@ "Val is:% i", valorcinq);

Within the field campoHoraCinq time I put the value 2, most log me in
returns another value (4782560). Why?

Thanks
 
Try this:
Code:
NSLog([NSString stringWithFormat:@"Val is:% d", valorcinq]);


*edit*
Didn't even catch what dejo pointed out below. The code you have, you're not even going to get the number out of it. You need to get the int value of the string first. This is programming 101 stuff here for the most part.
 
I have one more question:
When I do the command;

valorcinq int = (int) [campoHoraCinq text];
NSLog (@ "Val is:% i", valorcinq);

Within the field campoHoraCinq time I put the value 2, most log me in
returns another value (4782560). Why?
I take it you are intending to convert the text field into an int, with this line?:
Code:
valorcinq int = (int) [campoHoraCinq text];
If so, you can't just cast an NSString (presumably that's what [campoHoraCinq text] returns, right?) into an int. Check the class reference for NSString and look around the int... instance methods it has.
 
Try this:
Code:
NSLog([NSString stringWithFormat:@"Val is:% d", valorcinq]);


*edit*
Didn't even catch what dejo pointed out below. The code you have, you're not even going to get the number out of it. You need to get the int value of the string first. This is programming 101 stuff here for the most part.

I do not understand why it seems easy to me get a field, put in a variable and display on screen.

Find that the value is wrong after making the conversion to int, using this command:

valorcinq int = (int) [campoHoraCinq text];

Pordo someone please explain me what's wrong?

I tried to use the NSLog ([NSString stringWithFormat: @ "Val is:% d", valorcinq]), plus the value is already been printed the wrong conversion.

Thanks again
 
I do not understand why it seems easy to me get a field, put in a variable and display on screen.

Find that the value is wrong after making the conversion to int, using this command:

valorcinq int = (int) [campoHoraCinq text];

Pordo someone please explain me what's wrong?

I tried to use the NSLog ([NSString stringWithFormat: @ "Val is:% d", valorcinq]), plus the value is already been printed the wrong conversion.

Thanks again

NSString is an object. an object is just that - an object. you cannot convert it to an int.
look at it this way: would you expect a result if you converted a UIView into an int, or a UILabel? Nope.
an object is a pointer to a memory adress. and you cannot convert memory adresses.

If you look at the reference of NSString though, you will see that NSString has a method "intValue" which returns the text of the NSString as an int. This is what you need.
 
NSString is an object. an object is just that - an object. you cannot convert it to an int.
look at it this way: would you expect a result if you converted a UIView into an int, or a UILabel? Nope.
an object is a pointer to a memory adress. and you cannot convert memory adresses.

If you look at the reference of NSString though, you will see that NSString has a method "intValue" which returns the text of the NSString as an int. This is what you need.

Hello BlacWolf,

Thanks for the explanation.
Now I understand the problem and could solve my code.

Below the code if someone who is starting out;

Code:
NSString *strValor = [campoHoraCinq text];
int valorConvertido = [strValor intValue];

NSLog(@"Campo: %i", valorConvertido);

Thanks for the help of all;)
 
Hello BlacWolf,

Thanks for the explanation.
Now I understand the problem and could solve my code.

Below the code if someone who is starting out;

Code:
NSString *strValor = [campoHoraCinq text];
int valorConvertido = [strValor intValue];

NSLog(@"Campo: %i", valorConvertido);

Thanks for the help of all;)

yep, that's the way to do it.
btw, you can also use
Code:
int valorConvertido = [[campoHoraCinq text] intValue];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.