-1

誰かが私が取り組んできた問題を解決してくれることを願っています...

MapBox を使用して地図ベースのアプリを開発し、各地図注釈に NSMutableDictionary をアタッチして追加データを保存したいと考えています。私はそれを機能させましたが、XCode は私のデータ/オブジェクト型のいくつかについて警告を出し続けたので、それらを調べて片付けましたが、今は壊れています。アイデアは、ViewDidLoad で、プログラムが plist 辞書のセットを実行して各注釈を正しく設定するというものです。最初の anno マーカーが正しい設定でポップアップするため、それでも問題なく実行されます。ただし、毎回 plist に戻るのではなく、各注釈の userinfo プロパティにディクショナリをアタッチして、選択データの切り替えやその他の機能に使用できるようにしたいと考えています。これが私のコードです:

NSDictionary *ExploreSteps = [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ExploreSteps" ofType:@"plist"]];
for (NSString *key in [ExploreSteps allKeys])
    {
        //Loop through keys for each anno
        NSDictionary *thisStep = [ExploreSteps objectForKey:key];
        NSNumber *annoIndex = [thisStep objectForKey:@"Index"];
        NSNumber *isLive = [thisStep valueForKey:@"isLive"];
        NSString *annoTitle = [thisStep objectForKey:@"Title"];
        NSString *annoText = [thisStep objectForKey:@"Text"];
        NSString *imagefile = [thisStep objectForKey:@"Imagefile"];
        double longitude = [[thisStep objectForKey:@"Longitude"] doubleValue];
        double latitude = [[thisStep objectForKey:@"Latitude"] doubleValue];
        NSString *pagefile = [thisStep objectForKey:@"Pagefile"];
        NSString *audiofile = [thisStep objectForKey:@"Audiofile"];
        CLLocationCoordinate2D annoCoord = CLLocationCoordinate2DMake(latitude, longitude);

        RMAnnotation *annotation = [[RMAnnotation alloc] initWithMapView:mapView coordinate:annoCoord andTitle:annoTitle];
        annotation.annotationIcon = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:imagefile ofType:@"png"]]; 
        annotation.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:annoIndex, @"index", isLive, @"isLive", annoTitle, @"annoTitle", annoText, @"annoText", imagefile, @"imagefile", pagefile, @"pagefile", audiofile, @"audiofile", nil];
        NSLog(@"Title: %@",[annotation.userInfo objectForKey:@"annoTitle"]);
        [mapView addAnnotation:annotation];
    }

NSLog は annoTitle 文字列を吐き出す必要がありますが、代わりに毎回 null が返され、アプリの残りの動作からも、辞書に保存されている情報が単純に「通過」していないことがわかります。

何か案は?前もって感謝します!

ETA: 辞書を初期化するための修正されたコード (問題に違いがあるようには見えません!):

NSMutableDictionary *myUserInfo = [[NSMutableDictionary alloc] initWithObjectsAndKeys:annoIndex, @"index", isLive, @"isLive", annoTitle, @"annoTitle", annoText, @"annoText", imagefile, @"imagefile", pagefile, @"pagefile", audiofile, @"audiofile", nil];
        annotation.userInfo = myUserInfo;
        NSLog(@"Title: %@",[annotation.userInfo objectForKey:@"annoTitle"]);
        NSLog(@"Length: %u",[[annotation.userInfo allKeys] count]);

(Title は "(null)" を返しますが、Length は "1" を返します。これが役に立ったら...)

4

2 に答える 2

1

ほぼ確実に、あなたのオブジェクトの 1 つは ですnil。あなたはそれallKeys] count]が戻る1と述べたので、さらに進んで、あなたの値は であると言うことができisLiveますnil。したがって、元の行:

[NSMutableDictionary dictionaryWithObjectsAndKeys:annoIndex, @"index", isLive, @"isLive", annoTitle, @"annoTitle", annoText, @"annoText", imagefile, @"imagefile", pagefile, @"pagefile", audiofile, @"audiofile", nil];

以下とまったく同じように動作します。

[NSMutableDictionary dictionaryWithObjectsAndKeys:annoIndex, @"index", nil, @"isLive", annoTitle, @"annoTitle", annoText, @"annoText", imagefile, @"imagefile", pagefile, @"pagefile", audiofile, @"audiofile", nil];

そして、辞書はannoIndex最終的なキーと値のペアになります。

おそらく、変更可能なコピーを取得して、不要なthisStepキーを取り除き、それをuserInfo.

于 2012-12-10T05:33:53.183 に答える
0

これは、userInfo の NSMutableDictionary を作成する方法です。[[NSMutableDictionary alloc] initWithObjects:...] と [NSMutableDictionary dictionaryWithObjects:...] の違いを見てください。

" +dictionaryWithObjects: 自動解放された辞書を返します -initWithObjects: 自分自身を解放する必要があります

辞書をインスタンス変数として存続させたい場合は、init メソッドを使用して辞書を作成するか、自動解放されたバージョンを保持する必要があります。いずれにしても、dealloc メソッドで必ず解放する必要があります。

于 2012-12-10T01:24:39.843 に答える