How to convert String to double with point? - xcode

I'm currently working on an app that works with locations and therefore I have the following code:
CLLocationCoordinate2d coord;
coord.longitude = [longitude doubleValue];
coord.latitide = [latitude doubleValue];
Seems legit, doesn't it?
Now longitude and latitude are STRINGS like "10.112233". I know that normally double hasn't got points in there, but the cllocationcoordinate2d wants it like that...
Now, if you NSLog the strings, they work just fine, but if you NSLog the doubleValues it simply return nothing. How can I fix that?

The following should work:
NSString *coordinateAsString = #"10.112233";
coord.longitude = [[NSDecimalNumber decimalNumberWithString:coordinateAsString] doubleValue];

Can you try using NSDecimalNumber like following:
NSString *string = #"10.112233";
NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:string];

Related

How to use "valueforkey"?

I'm trying to do the following - I have an Array in which some strings are stored. These strings shall be used to call an NSArray. An example will clarify what I'm trying to do:
This is the working code that I'm trying to achieve ("briefing0" is of type NSArray):
NSString *path = [docsDir stringByAppendingPathComponent:[briefing0 objectAtIndex:indexPath.row]];
This is the "same" code that I'm trying to use:
int i = 0;
NSString *path = [docsDir stringByAppendingPathComponent:[(NSArray *)[NSString stringWithFormat:#"briefing%d", i] objectAtIndex:indexPath.row]];
Any ideas?
Thanks in advance!
Tom
Assuming that briefing0 is actually a property, then yes, this is possible (and not uncommon) in ObjC via KVC.
int i = 0;
NSString *prop = [NSString stringWithFormat:#"briefing%d", i];
NSArray *array = [self valueForKey:prop];
NSString *value = [array objectAtIndex:indexPath.row];
... etc. ...
-valueForKey: is the piece you're looking for. Note that this will throw an exception if you construct a key that does not exist, and so must be used with extreme care.

Convert NSString to few NSIntegers

I have getting data from plist to NSString, in result I see something like this "{{1848,594},{154,176}}". What is the best way to convert every single number to separate NSInteger?
NSString *frame = [myPlistKey objectForKey:#"frame"];
How to convert frame to 4 separate integers?
In this specific case, it looks like you are trying to parse the string representation of an NSRect, in which case you can just use NSRectFromString() from the Foundation framework.
Edit:
Since you are not much specific, I will try to cover your situation. If i count with the fact you have NSString *frame filled with {{1848,594},{154,176}}:
NSString *stringWithoutLeftBracket = [frame
stringByReplacingOccurrencesOfString:#"{" withString:#""];
NSString *stringWithoutRightBracket = [stringWithoutLeftBracket
stringByReplacingOccurrencesOfString:#"}" withString:#""];
NSArray *frameArray = [stringWithoutRightBracket componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#","]];
Then you can access objects with indexes like:
NSInteger integer = [[frameArray objectAtIndex:0] integerValue];
But also you could use a for loop like this:
for (NSInteger integer in frameArray) {
// Do something
}
In my opinion you have a string made from rect, means you could convert it.

Xcode Add (Math) Label and Textfield

Hi I am trying to add (math not put letters together) a textfield's data, and a label's data together. I have preformed the same code you see below for multiplication but when I do the same (A little changed) it does not seem to work. The code I placed below just shows the value of the billAmountTextField. Do you know why? Thanks for your help!
-(IBAction)totalAllUp:(id)sender
{
NSString *billTotalAmountString = [billAmountTextField text];
float billTotalFloat =[billTotalAmountString floatValue];
NSString *tipTotalAmountString = [resultLabel text];
float tipTotalAmountFloat = [tipTotalAmountString floatValue];
float totalAmountAfterAdding = billTotalFloat + tipTotalAmountFloat;
NSString *finalAnswer = [NSString stringWithFormat:#"%0.2f", totalAmountAfterAdding];
[totalAfterAdd setText:finalAnswer];
}

Xcode TextField Shows Numbers but Code Adds Decimal

I am making tip calculator and this is the code I currently have: (Below). [To make it look nice], is there a way for the user to just put in (15), and have code make that say .15 in the background? Thanks
- (IBAction)calcTapped:(id)sender {
NSString *billAmountString = [billAmountTextField text];
float billAmountFloat = [billAmountString floatValue];
NSString *tipPercentString = [tipPercentTextField text];
float tipPercentFloat = [tipPercentString floatValue];
float tipAmount = billAmountFloat * tipPercentFloat;
NSString *result = [NSString stringWithFormat:#"Tip: %0.2f",
tipAmount];
[resultLabel setText:result];
}
Try retrieving the string from the textfield, converting it into a NSNumber (or float) and then dividing by 100. You know, since 15/100 = 0.15.

converting a CLLocationCoordinate2D type to number or string

I was wondering how to convert latitude and longitude values of CLLocationCoordinate2D to numbers or string values.
Iver tried a few different ways but they arene't working:
CLLocationCoordinate2D centerCoord;
centerCoord.latitude = self.locModel.userLocation.coordinate.latitude ;
centerCoord.longitude = self.locModel.userLocation.coordinate.longitude;
NSString *tmpLat = [[NSString alloc] initWithFormat:#"%g", centerCoord.latitude];
NSString *tmpLong = [[NSString alloc] initWithFormat:#"%g", centerCoord.longitude];
NSLog("User's latitude is: %#", tmpLat);
NSLog("User's longitude is: %#", tmpLong);
This returns a warning by the compiler.
The warning is
warning: passing argument 1 of 'NSLog' from incompatible pointer type
How do I do this?
Any help would be appreciated.
thanks
You haven't mentioned what the warning is but it's most likely because you forgot the # in front of the NSLog strings:
NSLog(#"User's latitude is: %f", self.locModel.userLocation.coordinate.latitude );
NSLog(#"User's longitude is: %f", self.locModel.userLocation.coordinate.longitude );
Your updated code should be:
NSLog(#"User's latitude is: %#", tmpLat);
NSLog(#"User's longitude is: %#", tmpLong);
NSLog expects an NSString parameter which needs an # sign in front. Without the # sign, the string is a plain C string not an NSString object.

Resources