昨天發現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;
}