2010-12-04 37 views
3

这是来自Craig Schamp terremoto的一段代码..在模拟器(os 4.2)上工作正常,但不在设备上。iphone NSNumberFormatter在模拟器上工作不在设备操作系统4.2同时解析

self.currentEarthquake.magnitude = [formatter numberFromString:magString]; and 

NSNumber *latituide = [formatter numberFromString:[comp objectAtIndex:0]]; 

NSNumber *longitude = [formatter numberFromString:[comp objectAtIndex:1]] 

设备上是空的,而在模拟器已去所有数据....这里的所有代码....我的设备与4.2 3G是NSNumberFormatter问题???

if ([elementName isEqualToString:@"title"]) { 
    //<title>M 5.8, Banda Sea</title> 
    NSArray *components = [self.propertyValue componentsSeparatedByString:@","]; 
    if (components.count > 1) { 
    // strip the M 
    NSString *magString = [[[components objectAtIndex:0] componentsSeparatedByString:@" "] objectAtIndex:1]; 

    NSLog(@"String %@",magString); 

    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; 
    self.currentEarthquake.magnitude = [formatter numberFromString:magString]; 
    self.currentEarthquake.place = [components objectAtIndex:1]; 
    [formatter release]; 

    NSLog(@"Magnetudine %@",self.currentEarthquake.magnitude); 
    NSLog(@"Place %@",self.currentEarthquake.place); 

    NSLocale *currentUsersLocale = [NSLocale currentLocale]; 
    NSLog(@"Current Locale: %@", [currentUsersLocale localeIdentifier]); 

    } 
} else if ([elementName isEqualToString:@"updated"]) { 
    //<updated>2008-04-29T19:10:01Z</updated> 
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"]; 
    self.currentEarthquake.lastUpdate = [formatter dateFromString:self.propertyValue]; 
    [formatter release]; 

    NSLog(@"Date %@",self.currentEarthquake.lastUpdate); 

} else if ([elementName isEqualToString:@"georss:point"]) { 
    NSArray *comp = [self.propertyValue componentsSeparatedByString:@" "]; 
    NSLog(@"Comp %@",comp); 

    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; 


    NSNumber *latituide = [formatter numberFromString:[comp objectAtIndex:0]]; 
    NSLog(@"Latitude %@",latituide); 
    NSNumber *longitude = [formatter numberFromString:[comp objectAtIndex:1]]; 
    [formatter release]; 
    CLLocation *location = [[CLLocation alloc] initWithLatitude:latituide.floatValue 
       longitude:longitude.floatValue]; 
    self.currentEarthquake.location = location; 
    [location release]; 

    NSLog(@"Location %@",location); 
} else if([elementName isEqualToString:@"entry"]) { 
    [(id)[self delegate] performSelectorOnMainThread:@selector(addEarthquake:) 
      withObject:self.currentEarthquake 
      waitUntilDone:NO]; 
} 

2010-12-05 03:46:45.738 Terremoto [251:307]小样( “41.9022”, “12.4579” ) 2010-12-05 03:46:45.745 Terremoto [251 :307]纬度(null)

2010-12-05 03:46:45.847 Terremoto [251:307]地点< +0.00000000,+ 0.00000000> +/- 0.00m(速度-1.00 mps /当然-1.00) @ 05/12/10 03:46:45 GMT-03:00

2010-12-05 03:46:45.854 Terremoto [251:307] String 5.0

2010-12-05 03:46:45.864 Terremoto [251:307] Magnetudine(空)

2010-12-05 03:46:45.868 Terremoto [251:307]地方酒店Ergife

2010-12-05 03:46:45.871 Terremoto当前的语言:it_IT

2010-12-05 03:46:45.892 Terremoto [251:307]日期2010-10-14 14:35: 18 0000

2010-12-05 03:46:45.898 Terremoto [251:307]小样( “-6.1020”, “127.5017” )

设备日志

+0

你可以NSLog`组件吗? – 2010-12-04 21:19:07

回答

6

它不工作的原因很有可能是您所在区域(意大利语),该期“”不是有效的小数点分隔符。

致电-[NSNumberFormatter setDecimalSeparator:]将其设置为正确的值。

+0

非常感谢我怀疑设备当前本地的cos是它 - 它和模拟器en_US ...只需添加[formatter setDecimalSeparator:@“。”];并很好地工作......再次感谢你 – Valerio 2010-12-05 10:56:20

1

我在同一个上下文中(解析坐标)有同样的问题。我在iOS 4.3上。我使用的iPad和iPad都设置为德语,所以IMO的格式化程序应该以相同的方式运行,但事实并非如此。

我结束了使用此:

pi.latitude = [NSNumber numberWithDouble:[(NSString *)[coords objectAtIndex:0] doubleValue]]; 

这在模拟器和设备上工作。

相关问题