iOS 6中regionThatFits函数的兼容性问题
昨天发现App一个必挂的场景,跟踪了一下,发现挂在如下语句:
[cpp]
[self.mapView setRegion:[self.mapView regionThatFits:theRegion] animated:NO];
崩溃信息是:
[cpp]
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'Invalid Region <center:nan, nan span:nan, nan>'
不过挂掉的时候,theRegion变量是有效的,所以把代码抽离开以便跟踪:
[cpp]
MKCoordinateRegion fitRegion = [self.mapView regionThatFits:theRegion];
[self.mapView setRegion:fitRegion animated:NO];
跟踪发现,regionThatFits函数会将theRegion变量:
[cpp]
(MKCoordinateRegion) theRegion = {
center = {
latitude = 30.849
longitude = 117.749
}
span = {
latitudeDelta = 0.01
longitudeDelta = 0.01
}
}
转换为:
[cpp]
(MKCoordinateRegion) fitRegion = {
center = {
latitude = nan
longitude = nan
}
span = {
latitudeDelta = nan
longitudeDelta = nan
}
}
我很疑惑,不自禁想起一句程序员经典名言——“以前跑得好好的啊”。
于是我换了台iOS 5的设备尝试,发现是OK的,看来是iOS 6的兼容性问题。于是我把4.3 - 6.0的模拟器跑了一遍,发现都是OK的,只有装iOS 6的真机会导致该问题。
于是我临时稍作修改:
[cpp]
MKCoordinateRegion fitRegion = [self.mapView regionThatFits:theRegion];
if (isnan(fitRegion.center.latitude)) {
// iOS 6 will result in nan. 2012-10-15
fitRegion.center.latitude = theRegion.center.latitude;
fitRegion.center.longitude = theRegion.center.longitude;
fitRegion.span.latitudeDelta = 0;
fitRegion.span.longitudeDelta = 0;
}
补充:移动开发 , IOS ,