解析时,iphone NSNumberFormatter在模拟器上工作,而不是在设备os 4.2上工作

时间:2010-12-04 21:09:30

标签: iphone nsnumberformatter

这是来自Craig Schamp的一段terremoto代码..在模拟器(os 4.2)上工作正常但在设备上没有。

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

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

NSNumber *longitude = [formatter numberFromString:[comp objectAtIndex:1]] 
设备上的

为空,而在模拟器上有所有数据....这里是所有代码....我的设备是3g,4.2 是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] Comp(     “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]位置&lt; +0.00000000,+ 0.00000000&gt; +/- 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(null)

2010-12-05 03:46:45.868 Terremoto [251:307] Place Hotel Ergife

2010-12-05 03:46:45.871 Terremoto [251:307]当前区域设置: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] Comp(     “-6.1020”,     “127.5017” )

设备日志

2 个答案:

答案 0 :(得分:6)

它不起作用的原因很可能是在您的语言环境(意大利语)中,句号“。”不是有效的小数分隔符。

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

答案 1 :(得分:1)

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

我最终使用了这个:

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

适用于模拟器和设备。