data sharing between views in iphone - 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];

Related

Formatting float values

This is probably a stupid question but anyway.
I wan the number I set on my label to be formated nicely like this 20,000,000 .
How do I do this ?
For now I've set the number of decimal points to 0 so I just get the whole number without any places.
[NSString stringWithFormat:#"%.0f", slider.value];
you can use the following formatter:
-(void)setCurrencyFormat
{
NSNumberFormatter *CurrencyFormat = [[NSNumberFormatter alloc] init];
[CurrencyFormat setNumberStyle:NSNumberFormatterCurrencyStyle];
label.text= [CurrencyFormat stringFromNumber:[NSNumber numberWithDouble:billAmountInDouble]];
NSString *st1=[NSString stringWithFormat:#"%#",[CurrencyFormat stringFromNumber:[NSNumber numberWithDouble:individualTaxInDouble]]];
label.text=st1;
}
Check out the docs for NSNumberFormatter - you can do pretty much everything with that.

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

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

Differing NSNumberFormatter behavior

Consider this code:
NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
nf.numberStyle = NSNumberFormatterCurrencyStyle;
NSLocale *l = [[NSLocale alloc] initWithLocaleIdentifier:#"it_IT"];
nf.locale = l;
[l release];
nf.decimalSeparator = #",";
nf.currencySymbol = #"US$";
[nf setLenient:YES];
NSString *s = #"US$ 0,05";
double d = [[nf numberFromString:s] doubleValue];
NSLog(#"%.2f",d);`
If I execute this in a normal Cocoa console-based application (10.6 sdk), I get the output "0.05" to the debug console.
However, if I execute this in the iPhone environment (OS 3.1.3), I get "0.00" output to the debug console. Am I doing something wrong with my usage of NSNumberFormatter? Or is this a discrepancy between the two platforms?
NSString *s = #"US$ 0,05";
What if you remove the space?