2011-05-04 56 views
0

我嘗試運行我的應用程序,但我有拋出異常,在控制檯中我有這樣的:終止所謂的「NSException」

2011-05-05 00:18:50.984 myApp[2906:207] *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x6b8b780> was mutated while being enumerated.(
    "<MyLocation: 0x6b67af0>", 
    "<MyLocation: 0x6b19360>", 
    "<MyLocation: 0x6b67a70>", 
    "<MyLocation: 0x6b8d110>", 
    "<MyLocation: 0x6b8d280>", 
    "<MyLocation: 0x6b8ce50>", 
    "<MyLocation: 0x6b8d660>" 
)' 
*** Call stack at first throw: 
(
    0 CoreFoundation      0x02915919 __exceptionPreprocess + 185 
    1 libobjc.A.dylib      0x0272a5de objc_exception_throw + 47 
    2 CoreFoundation      0x029153d9 __NSFastEnumerationMutationHandler + 377 
    3 myApp       0x00005755 -[StationsSurLaCarteViewController requestFinished:] + 343 
    4 myApp       0x000195cb -[ASIHTTPRequest reportFinished] + 171 
    5 Foundation       0x000abe9a __NSThreadPerformPerform + 251 
    6 CoreFoundation      0x028f6d7f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15 
    7 CoreFoundation      0x028552cb __CFRunLoopDoSources0 + 571 
    8 CoreFoundation      0x028547c6 __CFRunLoopRun + 470 
    9 CoreFoundation      0x02854280 CFRunLoopRunSpecific + 208 
    10 CoreFoundation      0x028541a1 CFRunLoopRunInMode + 97 
    11 GraphicsServices     0x02e8e2c8 GSEventRunModal + 217 
    12 GraphicsServices     0x02e8e38d GSEventRun + 115 
    13 UIKit        0x0033ab58 UIApplicationMain + 1160 
    14 myApp       0x0000224c main + 102 
    15 myApp       0x000021dd start + 53 
) 
terminate called after throwing an instance of 'NSException' 
Program received signal: 「SIGABRT」. 
(gdb) 

我注意到,我得到了自我嘗試使用標註爲我的地圖視圖,請幫助,THX提前:)

編輯

這是我for循環,可以使問題:

for (int i=0; i<[array count]; i++) { 

      NSDictionary *stationEnCours=[array objectAtIndex:i]; 


      NSString *distance=[stationEnCours objectForKey:@"distance"]; 
      float lng = [[stationEnCours objectForKey:@"ssiphone_longitude"] floatValue]; 
      float lat = [[stationEnCours objectForKey:@"ssiphone_latitude"] floatValue]; 
      NSString *ensStation=[stationEnCours objectForKey:@"ssiphone_enseigne"]; 

      location2D = (CLLocationCoordinate2D){ .latitude = lat, .longitude = lng }; 
      MyLocation *annotation=[[[MyLocation alloc]initWithName:ensStation distanceVersLaStation:distance coordinate:location2D]autorelease]; 
      [mapView addAnnotation:annotation]; 
      MKCoordinateSpan span={latitudeDelta:0.2,longitudeDelta:0.2}; 
      MKCoordinateRegion region={location2D,span}; 
      [mapView setRegion:region]; 
      [self.view addSubview:mapView]; 
      }` 

回答

1

您例外的給定原因是「集合...被列舉時發生了變化」。這意味着您試圖更改for(in)循環內的數組,這是不允許的,因爲它會更改您應該枚舉的對象。原因的結尾是您正在枚舉的數組的轉儲。它包含7個MyLocation對象。如果您查看調用堆棧的頂部,您會發現異常發生在您的-[StationsSurLaCarteViewController requestFinished:]方法中。如果您仔細查看該方法並找到使用快速枚舉的地方,則應該很容易找到問題所在。

如果要在使用快速枚舉的同時修改數組,則有兩種可能性:跟蹤要更改的所有內容(即要刪除的索引列表)並在循環後進行更改,或者請在數組的副本上進行枚舉。這裏是第二種方法的例子:

NSArray *copiedArray = [originalArray copy]; 
for(id theObject in copiedArray) { 
    if([theObject shouldBeDeleted]) { 
     [originalArray removeObject:theObject]; 
    } 
} 
[copiedArray release]; 
NSArray *copiedArray = [originalArray copy]; 
for(id theObject in copiedArray) { 
    if([theObject shouldBeDeleted]) { 
     [originalArray removeObject:theObject]; 
    } 
} 
[copiedArray release]; 
+0

嗨,請看看我的編輯,我看不到'for'loop有什麼問題,我是否缺少內存管理? – Luca 2011-05-05 11:48:09

+0

你確定循環導致崩潰嗎?你正在尋找的那個應該使用'for(in)'語法,並且改變以'in'命名的數組。如果該循環是問題,那麼另一個循環運行在另一個也是負責的線程中。 – ughoavgfhw 2011-05-05 16:47:57

相關問題