开发者

adding entries to NSMutableDictionary

开发者 https://www.devze.com 2023-03-29 03:35 出处:网络
I\'m having trouble with adding dictionary\'s to a nsmutabledictionary. Can anyone see what I\'m doing wrong?

I'm having trouble with adding dictionary's to a nsmutabledictionary. Can anyone see what I'm doing wrong?

@interface viewMap : UIViewController<MKMapViewDelegate> {

     NSMutableDictionary *onclickDic;

}

@property (nonatomic, retain) NSMutableDictionary *onclickDic;
@end

@implementation viewMap
@synthesize onclickDic;

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {

    NSString *pushLat   = [NSString stringWithFormat:@"%f", [annotation coordinate].latitude];
    NSString *pushLng   = [NSString stringWithFormat:@"%f", [annotation coordinate].longitude];

    NSDictionary *latlngDic = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:pushLat, pushLng, nil] forKeys:[NSArray arrayWithObjects:@"lat", @"lng", nil]];

    NSDictionary *toPush    = [NSDictionar开发者_开发问答y dictionaryWithObject:latlngDic forKey:[NSString stringWithFormat:@"%i", i]];


    NSLog(@"toPush is %@", toPush); // this one is correct and works

    [self.onclickDic addEntriesFromDictionary:toPush];

    NSLog(@"onclickDic is %@", onclickDic); // this one gives (null)
}
@end


It looks like you don't ever create the onclickDic. You also never release it.

Try adding these methods:

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle
{
    self = [super initWithNibName:nibName bundle:nibBundle];
    if (self)
    {
        onclickDict = [[NSMutableDictionary alloc] init];
    }
    return self;
}

- (void)dealloc
{
    [onclickDict release];

    [super dealloc];
}


It doesn't look like onclickDic is ever assigned. Make sure you are allocating an instance before your mapView:viewForAnnotation: method gets called.

Also, not related to the problem, but your toPush dictionary is unnecessary. Just add the key/value to onclickDic directly:

[onclickDic setValue:latlngDic forKey:[NSString stringWithFormat:@"%i", i]];


You do not seem to instantiate self.onclickDic anywhere. Even with @synthesize, this is not done for you. Best place for that is probably init.

The reason the call is not failing, is that it's OK in Objective C to call a function on a nil object. This is commonly used when calling methods of delegates, for example.

0

精彩评论

暂无评论...
验证码 换一张
取 消