How do I convert the value of a UITextField to an NSNumber? - iphone

I have a small problem with iPhone SDK. I have 4 values in my xib, 4 UITextFields where I insert hostname, description, name and PORT.
The Port is an NSNumber. I must convert text (UITextField) into NSNumber but I don't know how.
I tried the following:
NSNumber *temp = [[NSNumber alloc] initWithString:portTextField.text];
serverObj.port = temp;
but my app crashes. I don't have problems with NSString (description ecc.)

NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:textField.text];
[f release];
And that should get the job done.

Just use the 'integerValue' method of NSString, to get a 'NSInteger'. Then you can create a NSNumber object from it.
serverObj.port = [ NSNumber numberWithInteger: [ portTextField.text integerValue ] ];

There is no method for NSNumber objects called initWithString: (that's why your app crashes). You need to use NSNumberFormatter's method numberFromString:

Related

data sharing between views in iphone

I want to share data ie some NSNumber from SetupMatchViewController to SetupTeamController
here is my code:
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
team.turnsInt=[f numberFromString:turnButton.currentTitle ];
team.perTurnInt=[f numberFromString:perTurnButton.currentTitle ];
team.breakInt=[f numberFromString:breakButton.currentTitle ];
But I am getting 0 value .
I am not getting the data I want.
if type of team.turnsInt is int, then you might do it with:
team.turnsInt=[[f numberFromString:turnButton.currentTitle ] intValue];

Showing formatted digits in textField iphone application

I am taking data from XMl file, the distance in xml is like
<distance>13.472987570222 km</distance>
Now i want to show just two digits after . operator. i.e i want to show in textField like 13.47 km. i have saved this distance digits in NSString *distance;
Thanks
float theDistance = [distance floatValue];
NSString *roundedDistance = [NSString stringWithFormat:#"%.2f",theDistance];
That will round to 2dp. :)
You can use very powerful class NSNumberFormatter:
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setPositiveFormat:#"##0.## km"];
[numberFormatter setNegativeFormat:#"##0.## km"];
NSNumber *number = [NSNumber numberWithDouble:[distance doubleValue]];
NSString *formattedString = [numberFormatter stringFromNumber:number];
For more info read here
Strikes me you should really be converting the xml string into a float or some other such appropriate type and then using a format specifier when displaying the value.

How to add an integer to an array?

This must be quite basic, but I was wondering how to add an integer to an array?
I know I can add strings like this:
NSMutableArray *trArray = [[NSMutableArray alloc] init];
[trArray addObject:#"0"];
[trArray addObject:#"1"];
[trArray addObject:#"2"];
[trArray addObject:#"3"];
But I guess I can't simply add integers like so:
NSMutableArray *trArray = [[NSMutableArray alloc] init];
[trArray addObject:0];
[trArray addObject:1];
[trArray addObject:2];
[trArray addObject:3];
At least the compiler isn't happy with that and tells me that I'm doing a cast without having told it so.
Any explanations would be very much appreciated.
Yes that's right. The compiler won't accept your code like this. The difference is the following:
If you write #"a String", it's the same as if you created a string and autoreleased it. So you create an object by using #"a String".
But an array can only store objects (more precise: pointers to object). So you have to create objects which store your integer.
NSNumber *anumber = [NSNumber numberWithInteger:4];
[yourArray addObject:anumber];
To retrive the integer again, do it like this
NSNumber anumber = [yourArray objectAtIndex:6];
int yourInteger = [anumber intValue];
I hope my answer helps you to understand why it doesn't work. You can't cast an integer to a pointer. And that is the warning you get from Xcode.
EDIT:
It is now also possible to write the following
[yourArray addObject:#3];
which is a shortcut to create a NSNumber. The same syntax is available for arrays
#[#1, #2];
will give you an NSArray containing 2 NSNumber objects with the values 1 and 2.
You have to use NSNumbers I think, try adding these objects to your array: [NSNumber numberWithInteger:myInt];
NSMutableArray *trArray = [[NSMutableArray alloc] init];
NSNumber *yourNumber = [[NSNumber alloc] numberWithInt:5];
[trArray addObject: yourNumber];
You can also use this if you want to use strings:
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[NSString stringWithFormat:#"%d",1]];
[[array objectAtIndex:0] intValue];

Correctly convert NSString to NSnumber

I can't figure out why it's not working, I have a NSString which I need to convert to NSNumber (to save it to Core Data)
e.g
NSLog(stringNum);
returns 1
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *myNumber = [f numberFromString:stringNum];
[f release];
NSLog(#"myNumber = %i", myNumber);
returns 120882496 or something like this
What am I missing?
Thanks for help
It's now an object, not an integer, therefore you must use %# in NSLog, not %i.
myNumber is an object, so the format should be
#"myNumber = %#"

Pass-by-value argument in message expression is undefined

I'm developing an iPhone application and I getting that warning at method:
NSNumber *latitudeValue;
NSNumber *longitudeValue;
[self obtainLatitude:latitudeValue longitude:longitudeValue];
The method is declared as follows:
- (void) obtainLatitude:(NSNumber *)latitudeValue longitude:(NSNumber *)longitudeValue {
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
latitudeValue = [f numberFromString:[latitude.text stringByReplacingOccurrencesOfString:#"," withString:#"."]];
longitudeValue = [f numberFromString:[longitude.text stringByReplacingOccurrencesOfString:#"," withString:#"."]];
[f release];
}
As you can see, I'm trying to calculate latitudeValue and longitudeValue calling obtainLatitude:longitude: but I'm doing something wrong.
How can I fix that error?
Elfred's answer works, but pass-by-reference for non-NSError** parameters is pretty uncommon. As well, coordinates -- numeric values, in general -- are most typically stored in regular old C types in structures because, comparatively, an NSNumber is quite a bit of overhead (no big deal for a few of 'em, would be a problem if you have a few dozen, hundred, or thousands of coordinates).
Something like:
struct MyLocation {
CGFloat latitude;
CGFloat longitude;
};
typedef struct MyLocation MyLocation;
Then:
- (MyLocation) mapCoordinates {
MyLocation parsedLocation;
parsedLocation.latitude = ....;
parsedLocation.longitude = ....;
return parsedLocation;
}
Something like the above would be more typical in an iPhone/Cocoa program.
As Dave points out, you really don't need to define your own type for this. Use CLLocationCoordinate2D or CLLocation.
You are indeed passing the pointers by value, so when you reassign them, that just takes effect inside thee method. One alternative is to do the following:
- (void) obtainLatitude:(NSNumber **)latitudeValue longitude:(NSNumber **)longitudeValue {
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
*latitudeValue = [f numberFromString:[latitude.text stringByReplacingOccurrencesOfString:#"," withString:#"."]];
*longitudeValue = [f numberFromString:[longitude.text stringByReplacingOccurrencesOfString:#"," withString:#"."]];
[f release];
}
then your call would look like:
NSNumber *latitudeValue;
NSNumber *longitudeValue;
[self obtainLatitude:&latitudeValue longitude:&longitudeValue];