Reputation: 11
How I can scan a number from a text field?
For example, in C
int x,area;
scanf("%d",&x);
area=r*r*3.14;
printf(" the area is %d",area);
How to do this on iPhone in Objective-C with a text field?
Upvotes: 1
Views: 409
Reputation: 5687
if it's in a textfield, you can do
double r=[textFieldName.text doubleValue];
NSString *result = [NSString stringWithFormat:@"the area is %f",r*r*3.14];
someLabelYouAreUsingToDisplayResults.text=result;
Upvotes: 2
Reputation: 106117
What you wrote works perfectly fine in Objective-C (or rather would, if it wasn't buggy). Everything that works in C works in Objective-C, without modification.
That said, there are more idiomatic ways to do such things in the Objective-C language (and Cocoa libraries), which I'm sure others will point out to you. See Robot Woods's answer for an idiomatic example of how to read from a text field, for instance.
Upvotes: 1
Reputation: 39905
Check out NSScanner. It performs a similar function to scanf, but works on NSString objects.
Upvotes: 2