iphone NSNumberFormatter 在解析时在模拟器上工作,而不是在设备操作系统 4.2 上工作
这是 Craig Schamp 的一段 terremoto 代码。在模拟器(操作系统 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] 比较 ( “41.9022”, “12.4579” ) 2010-12-05 03:46:45.745 Terremoto[251:307] 纬度(空)
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 (null)
2010-12-05 03:46:45.868 Terremoto[251:307] 埃尔吉夫广场酒店
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]比较( “-6.1020”, “127.5017” )
设备日志
This is a piece of code of terremoto from Craig Schamp .. works fine on simulator (os 4.2) but not on device.
self.currentEarthquake.magnitude = [formatter numberFromString:magString]; and
NSNumber *latituide = [formatter numberFromString:[comp objectAtIndex:0]];
NSNumber *longitude = [formatter numberFromString:[comp objectAtIndex:1]]
on device are null while on simulator have all the data .... here the all code ....my device is 3g with 4.2
is NSNumberFormatter the problem ???
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] Latitude (null)
2010-12-05 03:46:45.847 Terremoto[251:307] Location <+0.00000000, +0.00000000> +/- 0.00m (speed -1.00 mps / course -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] Current Locale: it_IT
2010-12-05 03:46:45.892 Terremoto[251:307] Date 2010-10-14 14:35:18 +0000
2010-12-05 03:46:45.898 Terremoto[251:307] Comp (
"-6.1020",
"127.5017"
)
device log
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它不起作用的原因很可能是在您的语言环境(意大利语)中,句点“.”。不是有效的小数点分隔符。
调用
-[NSNumberFormatter setDecimalSeparator:]
将其设置为正确的值。The reason it doesn't work is most likely that in your locale (Italian), the period "." is not the valid decimal separator.
Call
-[NSNumberFormatter setDecimalSeparator:]
to set it to the correct value.我在相同的上下文中遇到了相同的问题(解析坐标)。我使用的是 iOS 4.3。我使用的 Mac 和 iPad 都设置为德语,因此我认为格式化程序应该以相同的方式运行,但事实并非如此。
我最终使用了这个:
它适用于模拟器和设备。
I had the same issue in the same context (parsing coordinates). I'm on iOS 4.3. My mac an the iPad I used are both set to german, so IMO the formatter should behave the same way, but it didn't.
I ended up with using this:
That works on the simulator and on the device.