0

やあ、私は次のように構成された plist から読み込んでいます:

物件一覧

ご覧のとおり、さまざまなタイプ(C、Visitor)の注釈情報を含む plist です。各注釈タイプを問題なく表示できますが、すべてのタイプをループして、マップ ビューにすべての注釈を一度に表示しようとしています。コードは次のとおりです。

NSLog(@"loadAnnotations");
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"PermitData" ofType:@"plist"];
NSDictionary *rootOfPermitDataPlistDict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
// NSMutableDictionary *permitDict = [[NSMutableDictionary alloc] init];
 if ([self title] == @"All Permits") {
  for (id key in rootOfPermitDataPlistDict) {
   NSLog(@"key:%@",key);

   //[key retain];
   NSMutableDictionary *permitDict = [NSDictionary dictionaryWithDictionary:[rootOfPermitDataPlistDict objectForKey:key]];
   //[key release];

   //array containing annotation information: latitude, longitude, title, subtitle(see PermitData.plist)
   NSArray *annotationsArray = [[NSArray alloc] initWithArray:[permitDict objectForKey:@"annotations"]];
   [permitDict release];
   [rootOfPermitDataPlistDict release];

   CLLocationCoordinate2D workingCoordinate;
   NSDictionary *annotationContainerDict = [[NSDictionary alloc] init];
   //loop through annotations array, creating parking annotations filled with the information found in the plist
   for(annotationContainerDict in annotationsArray){
    NSLog(@"%@",annotationContainerDict);

    ParkingAnnotation *parkingAnnot = [[ParkingAnnotation alloc] init];
    workingCoordinate.latitude = [[annotationContainerDict objectForKey:@"latitude"] doubleValue];
    workingCoordinate.longitude = [[annotationContainerDict objectForKey:@"longitude"] doubleValue];
    [parkingAnnot setCoordinate:workingCoordinate];
    [parkingAnnot setTitle:[annotationContainerDict objectForKey:@"title"]];
    [parkingAnnot setSubtitle:[annotationContainerDict objectForKey:@"subtitle"]];
    if ([parkingAnnot title] == @"C Parking") [parkingAnnot setAnnotationType:annotationTypeC];
    else if ([parkingAnnot title] == @"Visitor Parking") [parkingAnnot setAnnotationType:annotationTypeVisitor];
    [mapView addAnnotation:parkingAnnot];
    [parkingAnnot release];
   }
   [permitDict release];
  }
 }

これは、プログラムを実行したときのコンソール出力です。

2010-11-25 03:25:28.020 Parking[38918:207] All Permits
2010-11-25 03:25:28.021 Parking[38918:207] loadAnnotations
2010-11-25 03:25:28.021 Parking[38918:207] key:C
2010-11-25 03:25:28.021 Parking[38918:207] {
    latitude = "38.545301";
    longitude = "-121.754066";
    subtitle = "VP 17";
    title = "C Parking";
}
2010-11-25 03:25:28.022 Parking[38918:207] {
    latitude = "38.544831";
    longitude = "-121.754785";
    subtitle = "VP 16";
    title = "C Parking";
}
2010-11-25 03:25:28.022 Parking[38918:207] {
    latitude = "38.544781";
    longitude = "-121.755729";
    subtitle = "VP 22";
    title = "C Parking";
}
2010-11-25 03:25:28.022 Parking[38918:207] {
    latitude = "38.544412";
    longitude = "-121.752489";
    subtitle = "VP 15";
    title = "C Parking";
}

したがって、最初の NSDictionary を適切にループしますが、次の NSDictionary をループし始めようとするとクラッシュします。NSDictionary を NSMutableDictionary に変更しようとしましたが、結果は同じです。そのため、テーブル ビューで [すべての許可] 行を選択すると、(マップ ビューに切り替えずに) 一瞬ハングした後、エラーを生成せずにクラッシュします。

ここで私を助けてくれる人がいれば、とても感謝しています。前もって感謝します!

編集:これがスタックトレースです(以下のコメントで説明されています):

2010-11-25 20:28:08.141 Parking[39400:207] All Permits
2010-11-25 20:28:08.142 Parking[39400:207] loadAnnotations
2010-11-25 20:28:08.142 Parking[39400:207] key:C
2010-11-25 20:28:08.143 Parking[39400:207] {
    latitude = "38.545301";
    longitude = "-121.754066";
    subtitle = "VP 17";
    title = "C Parking";
}
2010-11-25 20:28:08.143 Parking[39400:207] {
    latitude = "38.544831";
    longitude = "-121.754785";
    subtitle = "VP 16";
    title = "C Parking";
}
2010-11-25 20:28:08.143 Parking[39400:207] {
    latitude = "38.544781";
    longitude = "-121.755729";
    subtitle = "VP 22";
    title = "C Parking";
}
2010-11-25 20:28:08.144 Parking[39400:207] {
    latitude = "38.544412";
    longitude = "-121.752489";
    subtitle = "VP 15";
    title = "C Parking";
}
2010-11-25 20:28:08.145 Parking[39400:207] *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSCFDictionary: 0x6d5fd80> was mutated while being enumerated.<CFBasicHash 0x6d5fd80 [0x2667380]>{type = mutable dict, count = 1,
entries =>
    0 : <0x7380> = <NSKeyValueContainerClass: Original class: ParkingAnnotation, Notifying class: NSKVONotifying_ParkingAnnotation>
}
'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x025fdb99 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x0274d40e objc_exception_throw + 47
    2   CoreFoundation                      0x025fd659 __NSFastEnumerationMutationHandler + 377
    3   Parking                             0x00002e93 -[ParkingMapViewController loadAnnotations] + 364
    4   Parking                             0x00002caf -[ParkingMapViewController viewDidLoad] + 117
    5   UIKit                               0x0036a5ca -[UIViewController view] + 179
    6   UIKit                               0x003689f4 -[UIViewController contentScrollView] + 42
    7   UIKit                               0x003787e2 -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:] + 48
    8   UIKit                               0x00376ea3 -[UINavigationController _layoutViewController:] + 43
    9   UIKit                               0x00378067 -[UINavigationController _startTransition:fromViewController:toViewController:] + 326
    10  UIKit                               0x00372ccd -[UINavigationController _startDeferredTransitionIfNeeded] + 266
    11  UIKit                               0x00379d8b -[UINavigationController pushViewController:transition:forceImmediate:] + 876
    12  UIKit                               0x00372b67 -[UINavigationController pushViewController:animated:] + 62
    13  Parking                             0x00002914 -[PermitListViewController tableView:didSelectRowAtIndexPath:] + 307
    14  UIKit                               0x00333a48 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1140
    15  UIKit                               0x0032a32e -[UITableView _userSelectRowAtIndexPath:] + 219
    16  Foundation                          0x0003f21a __NSFireDelayedPerform + 441
    17  CoreFoundation                      0x025def73 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19
    18  CoreFoundation                      0x025e05b4 __CFRunLoopDoTimer + 1364
    19  CoreFoundation                      0x0253cdd9 __CFRunLoopRun + 1817
    20  CoreFoundation                      0x0253c350 CFRunLoopRunSpecific + 208
    21  CoreFoundation                      0x0253c271 CFRunLoopRunInMode + 97
    22  GraphicsServices                    0x02edc00c GSEventRunModal + 217
    23  GraphicsServices                    0x02edc0d1 GSEventRun + 115
    24  UIKit                               0x002ceaf2 UIApplicationMain + 1160
    25  Parking                             0x00001e08 main + 102
    26  Parking                             0x00001d99 start + 53
)
terminate called after throwing an instance of 'NSException'

ただし、 for ループ内で permitDict をリリースしています (2 番目の [permitDict release] はコメント解除されたままになっています)。ご覧のとおり、permitDict は NSMutableDictionary 型であるため、なぜこのエラーが発生するのかわかりません。

4

2 に答える 2

4

コードがクラッシュする主な理由は 2 つあります。

まず、for ループ内にある次の行:

[rootOfPermitDataPlistDict release];

現在ループしているオブジェクトそのものを破棄します。if ([self title]...それを一番最後、つまりステートメントの右中括弧の後に移動します。

2 番目の 2 行は次のとおりです。

[permitDict release];

削除する必要があります。自動解放されたオブジェクトを返す DictionaryWithDictionary を使用して作成しているため、permitDict を解放しないでください。

これら 2 つの変更により、コードが実行されるはずです。


ただし、他にもいくつかの問題があります。

  • 割り当て + 初期化しますannotationsArrayが、決して解放しません (メモリ リーク)。その配列を通過する for ループの後に解放します (現在 2 番目の がある場所[permitDict release];)。
  • 割り当て+初期annotationContainerDict化を行いますが、変数はannotationsArray内のオブジェクトへの参照としてのみ使用します(したがって、割り当てられたメモリを放棄します-メモリリーク)。わざわざ annotationContainerDict で alloc+init を実行する必要はありません。宣言するだけです。に変更NSDictionary *annotationContainerDict = [[NSDictionary alloc] init];しますNSDictionary *annotationContainerDict;
  • を使用して文字列を比較しています==isEqualToString:代わりに次のように使用します
    if ([[self title] isEqualToString:@"All Permits"])...。を使用する理由については、この質問この質問isEqualToStringを参照してください。
  • ここではそれほど深刻な問題ではありませんが、dictionaryWithDictionary を使用して、rootOfPermitDataPlistDict に既に存在するものを複製する必要はありません。次のように、埋め込み辞書への簡略参照として permitDict を使用できますNSMutableDictionary *permitDict = [rootOfPermitDataPlistDict objectForKey:key];
  • 注釈配列と同じ点: 新しい配列を割り当てて初期化する必要はありません。配列はすでに rootOfPermitDataPlistDict にあります。次のように参照を取得するだけですNSArray *annotationsArray = [permitDict objectForKey:@"annotations"];。このようにする場合は、annotationsArray を解放しないでください。

メモリ管理プログラミング ガイドでは、これらすべてについて詳しく説明しています。

于 2010-11-26T05:04:18.740 に答える
1

まず、あなたのコードには多くの問題がありましたが、これが 1 秒間に何百回もループする (多数の自動解放オブジェクトを作成する可能性がある) パフォーマンス クリティカルなコードでない限り、 alloc] init] メソッドを使用するとコードが従うのが難しい。(精神的に通過し、継続的に割り当てとリリースのバランスを取る必要があるため)。誤解しないでください。保持と解放などを理解することが重要ですが、これが私が問題に取り組む方法です。

あなたがコントロールしていることを忘れないでください。駐車場の注釈クラスが少し「自分で考える」ことができるようにします。以下の例では、 -(id)initWithDictionary: を追加しました。これにより、他のクラスがそこに座って、キーを 1 つずつ設定するという面倒な作業をすべて行う必要がなくなります。(空白を埋めると思われる架空の部分がいくつかあります...

NSString * const PPAnnotationsKey   = @"annotations";
NSString * const PPTitleKey         = @"title";
NSString * const PPSubtitleKey      = @"subtitle";
NSString * const PPLatitudeKey      = @"latitude";
NSString * const PPLongitudeKey     = @"longitude";

NSLog(@"loadAnnotations");
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"PermitData" ofType:@"plist"]; // autoreleased
NSDictionary *permitDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath]; // autoreleased
if ([[self title] isEqualToString:@"All Permits"]) {
    for (NSString *parkingGroup in permitDictionary) {
        NSLog(@"parkingGroup == %@", parkingGroup);
        NSArray *annotations = [parkingGroup objectForKey:PPAnnotationsKey];
        for (NSDictionary *entry in annotations) {
            PPParkingAnnotation *annotation = [PPParkingAnnotation parkingAnnotationWithDictionary:entry]; // autoreleased
            if (annotation) {
            // assuming here that mapView's addAnnotation: will retain the
            // annotation
            [mapView addAnnotation:annotation]; 
            }
        }
    }
}

@interface PPParkingAnnotation : NSObject <MKAnnotation> {
    CLLocationCoordinate2D   coordinate;
    NSString                *title;
    NSString                *subtitle;
}
+ (id)parkingAnnotationWithDictionary:(NSDictionary *)dictionary;
- (id)initWithDictionary:(NSDictionary *)dictionary;

@properties...
@end

@implementation PPParkingAnnotation

+ (id)parkingAnnotationWithDictionary:(NSDictionary *)dictionary {
    return [[[[self class] alloc] initWithDictionary:dictionary] autorelease];
}

- (id)initWithDictionary:(NSDictionary *)dictionary {
    [self setTitle:[dictionary objectForKey:PPTitleKey]];
    [self setSubtitle:[dictionary objectForKey:PPTitleKey]];
    // and so on.
}
于 2010-11-26T05:43:47.073 に答える