「nsexception」のインスタンスを投げた後に呼び出された終了
-
28-10-2019 - |
質問
私はアプリを実行しようとしますが、私は例外を投げました、コンソールで私はこれを持っています:
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];
}`
解決
あなたの例外の与えられた理由は、「コレクション...列挙されている間に変異した」ことです。これは、内部の配列を変更しようとしたことを意味します for( in )
ループ。これは、列挙されているはずのオブジェクトを変更するため、許可されていません。理由の終わりは、あなたが列挙していた配列のダンプです。 7が含まれています MyLocation
オブジェクト。コールスタックの上部を見ると、例外があなたの中で発生したことがわかります -[StationsSurLaCarteViewController requestFinished:]
方法。その方法を調べて、高速列挙を使用する場所を見つけた場合、問題を簡単に見つける必要があります。
高速列挙の使用中に配列を変更する場合、2つの可能性があります。変更するすべてのもの(つまり、削除するインデックスのリスト)を追跡し、ループ後に変更を行うか、列挙を実行するか、 a コピー 配列の。 2番目の方法の例は次のとおりです。
NSArray *copiedArray = [originalArray copy];
for(id theObject in copiedArray) {
if([theObject shouldBeDeleted]) {
[originalArray removeObject:theObject];
}
}
[copiedArray release];
所属していません StackOverflow