Correctly convert NSString to NSnumber - iphone

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 = %#"

Related

NSString to NSNumber convert

I try to convert a string :
3033547640189791162
with this method :
NSNumber * myNumber = [NSNumber numberWithDouble:[tmpId doubleValue]];
and this :
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterNoStyle];
NSNumber * myNumber = [f numberFromString:tmpId];
[f release];
and both of them give me this number :
<CFNumber 0x13a2b0 [0x3f54c9f8]>{value = +3033547640189791232.00000000000000000000, type = kCFNumberFloat64Type}
insted of :
<CFNumber 0x1b5550 [0x3f54c9f8]>{value = +3033547640189791232, type = kCFNumberSInt64Type}
Edit
When i try to use :
NSString *tmpId = #""3033547640189791162";
long i = [tmpId longLongValue];
i is equal to = -1631802438
int i = [tmpId intValue];
i is equal to = 2147483647
so i lose the original number value
If you want your NSNumber to hold an integer value you have to use [NSNumber numberWithInt:]:
NSNumber *myNumber = [NSNumber numberWithInt:[tmpId intValue]];
You should also consider to make sure, that your NSString contains a valid integer value:
NSScanner* scan = [NSScanner scannerWithString:tmpId];
int isInt;
if ( [scan scanInt:&isInt] && isInt ) {
NSLog(#"value is an integer: %i",isInt);
}
But there's a catch:
NSScanner validates the number inside the string as an integer, even though it should not … All ARM processors are 32-bit only, so the maximum range of an unsigned int is 0 to 4294967295.
So using NSNumberFormatter is the safest way to get the right value, because it handles finding the right data type for you:
NSString *tmpId = #"3033547640189791162";
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *myNumber = [f numberFromString:tmpId];
Moving my comment to answer:
Try NSNumber * myNumber = [NSNumber numberWithLongLong:[tmpId longLongValue]];

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 do I convert the value of a UITextField to an NSNumber?

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:

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