Зацикливаться через Nsdictionary, чтение от списка недвижимости Пропространение хлопот

StackOverflow https://stackoverflow.com/questions/4281983

Вопрос

Эй, ребята, я читаю от структурированной, как так:

property list

Как видите, это пл, который содержит информацию о аннотации различных типов (C, посетителей). Я могу отображать каждый тип аннотации просто отлично, но я пытаюсь закрутить все типы и отображать все аннотации на карте сразу. Вот код:

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";
}

Итак, правильно пестится через первый Nsdictictionary, но вылетает, когда речь идет о том, чтобы начать зацикливаться через следующую. И я попробовал меняться Nsdictionary на NSMutballyAry, но результат такой же. Поэтому, когда я выбираю строку «все разрешения» в табличном виде, она висит для доли секунды (без перехода на вид карты), а затем сбои, не генерируя ошибку.

Если кто-то не против помочь мне здесь, я бы очень признателен. Заранее спасибо!

Редактировать: вот трассировка стека (описана в комментариях ниже):

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'

Тем не менее, я освобождаю разрешительность в рамках цикла (второй разряд] был оставлен незаметным), поэтому я не знаю, почему жаловаться на Nsdictionary, будучи неизменной. Как видите, разрешение имеет тип NSMutballySasionAry, поэтому я потерян в том, почему он дает мне эту ошибку.

Это было полезно?

Решение

Код сбивается на две основные причины.

Первый, Эта линия, которая находится внутри петли:

[rootOfPermitDataPlistDict release];

Уничтожает самый объект, который вы в настоящее время зацикливаете. Переместите его до самого конца - после закрытия скобки if ([self title]... утверждение.

Второй, две строки, которые говорят:

[permitDict release];

следует удалить. Не выпускайте разрешение на разрешение, потому что вы создаете его, используя словарь, который возвращает аутоверенный объект.

С этими двумя изменениями код должен работать.


Однако, Есть несколько других проблем:

  • Вы Alloc + init annotationsArray Но никогда не выпускайте его (утечка памяти). Выпустите его после навязки, которая проходит через этот массив (где у вас в настоящее время есть второй [permitDict release];).
  • Вы Alloc + init annotationContainerDict Но затем используйте переменную только в качестве ссылки на объекты в AnnotizationAray (и поэтому отказаться от выделенной памяти - утечка памяти). Не беспокоиться о том, чтобы выделить Alloc + init на AnnotationContainerDict, просто заявляйте о нем. Сдача NSDictionary *annotationContainerDict = [[NSDictionary alloc] init]; чтобы просто NSDictionary *annotationContainerDict;.
  • Вы сравниваете строки, используя ==. Отказ Использовать isEqualToString: Вместо этого:
    if ([[self title] isEqualToString:@"All Permits"]).... Отказ Для объяснения того, почему вы должны использовать isEqualToString, видеть этот вопрос и этот вопрос.
  • Здесь не очень серьезная проблема, но не нужно пользоваться словарьювитным путем и, таким образом, дублировать то, что уже там в RootofPermitDataPListdict. Вы могли бы просто использовать разрешение в качестве сокращенного ссылки на встроенный словарь, подобный это: NSMutableDictionary *permitDict = [rootOfPermitDataPlistDict objectForKey:key];.
  • Один момент с AnnotionsationArray: Вам не нужно alloc + init новый массив. Массив уже находится в RootofPermitDataPListddict. Просто получите ссылку на это так: NSArray *annotationsArray = [permitDict objectForKey:@"annotations"];. Отказ Не выпускайте AnnotionationsArray, если вы решите сделать это таким образом.

То Руководство по управлению памятью Объясняет все это подробно.

Другие советы

Во-первых, было много проблем с вашим кодом, но если это не является эффективным критическим кодом, где вы будете циклировать сотни раз в секунду (что может потенциально может создать множество объектов AutoRelened), используя метод ALLOC] INIT, просто сделает код труднее следовать. (Поскольку вам придется мысленно пройти и постоянно сбалансировать alloC с выпуском). Не поймите меня неправильно, важно, чтобы вы понимаете сохранение и выпуск и т. Д. Но это так, как я бы подошел к проблеме.

Помните, что вы контролируете. Сделайте вашу парковку Annotation Class, чтобы «думать о себе» немного. В приведенном ниже примере я добавил INITITHITHISTARY INITITHITHISTARY: так, чтобы ваш другой класс не должен сидеть там и делать все воскресную работу установки клавиш один за другим. (Есть некоторые вымышленные части, где я бы предположил, что вы заполните пробелы ...

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.
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top