componentsSeparatedByString return wrong result - iphone

I used this code to cut the string
NSString *titleString = #"22.225453615805794,113.554006577014889";
NSArray *array = [titleString componentsSeparatedByString:#","];
NSLog(#"title string %#", titleString);
NSLog(#"first %.15f", [[array objectAtIndex:0] floatValue]);
NSLog(#"second %.15f", [[array objectAtIndex:1] floatValue]);
but why it return
22.225454330444336
and
113.554008483886719

Because floating point numbers are not that accurate, you can get a higher accuracy by calling doubleValue instead of floatValue:
NSLog(#"second %.15f", [[array objectAtIndex:1] doubleValue]);
This is not a problem with componentsSeparatedByString:.

I think there is problem in converting string into float. Try using double.

Related

NSString parsing

I need to parse this string into three different components:
Location: 1|#69.83623|#24.432223|#Cupertino, California
The value is stored in one NSString. I need it in three different strings. One string for latitude, one for longitude and one for location.
Any idea how I can do that?
Thanks!
You can use this method to get an array of different components:
NSArray *bits = [locationString componentsSeparatedByString: #"|#"];
Each item in the NSArray will be an NSString.
Try the following
NSString *location = #"1|#69.83623|#24.432223|#Cupertino, California";
NSArray *components = [location componentsSeparatedByString:#"|#"];
NSLog(#"%#",components);
float latitude = [[components objectAtIndex:1] floatValue];
float longitude = [[components objectAtIndex:2] floatValue];
NSString *loc = [components objectAtIndex:3];
NSString *t = #"Location: 1|#69.83623|#24.432223|#Cupertino, California";
NSArray *k = [t componentsSeparatedByString:#"|"];
NSLog(#"components %#", k);

How can I get an integer value from NSString in an iPhone application?

NSString * str=[zoneDict objectForKey:#"name"];
NSLog(#"==========string zone::==========%#",str);
// str="(GMT +3:00) Baghdad, Riyadh, Moscow, St. Petersbur";
How can I get the 3:00 value from the above string?
NSString *str = #"(GMT -3:00) Baghdad, Riyadh, Moscow, St. Petersbur";
NSRange endRange = [str rangeOfString:#")"];
NSString *timeString = [str substringWithRange:NSMakeRange(5, endRange.location-5)];
NSRange separatorRange = [timeString rangeOfString:#":"];
NSInteger hourInt = [[timeString substringWithRange:NSMakeRange(0, separatorRange.location)] intValue];
NSLog(#"Hour:%d",hourInt);
Rather than trying to extract the time offset from the string, is there any way you could store actual time zone data in your zoneDict? For example you could store NSTimeZone instances instead.
If all you have is the string, you could use an NSRegularExpression object and extract the relevant information using a regular expression instead.
If you could explain further what you're trying to do then there may be an alternative way to achieve what you want.
I like to use -[NSString componentsSeparatedByString]:
NSString *str = #"(GMT -3:00) Baghdad, Riyadh, Moscow, St. Petersbur";
NSArray *myWords = [myString componentsSeparatedByString:#")"];
NSString *temp1 = [myWords objectAtIndex:0];
if ([temp1 rangeOfString:#"-"].location == NSNotFound) {
NSArray *temp2 = [temp1 componentsSeparatedByString:#"+"];
NSString *temp3 = [temp2 objectAtIndex:1];
NSLog(#"Your String - %#", temp3);
}
else {
NSArray *temp2 = [temp1 componentsSeparatedByString:#"-"];
NSString *temp3 = [temp2 objectAtIndex:1];
NSLog(#"Your String - %#", temp3);
}
Output:
Your String - 3:00
Using regular expressions is the better option in my view (if you are forced to extract the '3' only). The regular expression string would contain something like "\d?" but don't quote me on that, you'll have to look up the exact string. Perhaps someone on here could provide the exact string.

Registration timer expired, but client is still registering! - Google geocoding error

I am trying to use these two lines to get lat/long info when given an address input.
NSString *urlString = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#&output=csv", theAddress];
NSString *locationString = [[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]] autorelease];
It is weird that sometimes I get the lat, long info as quick as in 5 seconds and in other cases the app is stuck for about 5 mins and then it logs an error stating "Registration timer expired, but client is still registering!"
It also gives the location info (ie result string wth lat/long info) as null.
I tried it a number of times and couldn't figure out what makes it work and what doesn't.
Has anybody encountered the same issue before or any advice on this pls.
Thanks
try below code its working fine with me.
CLLocationCoordinate2D coord=[self gettingLatandLonFromAddress:address];
-(CLLocationCoordinate2D ) gettingLatandLonFromAddress:(NSString *)address
{
CLLocationCoordinate2D currentLoc;
NSString *urlString=[NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#&output=csv",[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString=[NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:nil];
NSArray *listItems = [locationString componentsSeparatedByString:#","];
double latitude=0.0;
double longitude=0.0;
if([listItems count] >=4 && [ [listItems objectAtIndex:0] isEqualToString:#"200"]){
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
}
else{
NSLog(#"Error");
}
currentLoc.latitude=latitude;
currentLoc.longitude=longitude;
return currentLoc;
}

nsmutablearray float object returns a zero

In the root model I have:
[self.rPrices replaceObjectAtIndex:0
withObject:[NSNumber numberWithFloat:(float)nR4]];
NSLog(#"%.2f", [self.rPrices objectAtIndex:0]);
where rPrices is NSMutableArray.
nR4 is not zero but the above NSLog(...); displays zero.
Thanks
Try this
NSLog(#"%.2f", [[self.rPrices objectAtIndex:0] floatValue]);
or alternativly just print the NSNumber as an object
NSLog(#"%#", [self.rPrices objectAtIndex:0]);
NSNumber is an object. So you can't print it using float format specifier "%f".
You can use "%#" or get the float value from it using -floatValue and print it using "%f".

Problem when converting NSString to NSNumber in iPhone

I am having problem when converting string (YaxisData) to NSNumber. I have to return a NSNumber for Core-plot to get the graph done but its not working. Here's the sample code
-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index{
NSNumber *num = [NSNumber numberWithDouble:[[YaxisData objectAtIndex:index] doubleValue]];
return num;
}
num returns junk data such as -1 or 993494949494 but when I log the double value of number, it prints the correct value. I am not able to return this double value as the function signature requires only the NSNumber to be returned.
NSLog(#"Number: %f", [num doubleValue]);
I am stuck here and would really appreciate any help in this regard. Thanks!
Would this give a better result somehow?
NSString *aString = [YaxisData objectAtIndex:index];
NSLog(#"%#", aString);
double value = [aString doubleValue];
NSLog(#"%f", value);
NSNumber *number = [NSNumber numberWithDouble:value];
NSLog(#"%#", number);
If not, could you show the results of the NSLog()'s?
I know that in practice it seems the same code, yet one might not be so sure that -objectAtIndex: always returns a string. It might be a localization issue as well (commas and dots for decimal separator might get mixed up, this would definitely mess up your results. In case of a localization issue, check the following link:
How to convert an NSString into an NSNumber