为什么更改字典中包含的可变数组不需要更新字典? [英] Why does changing a mutable array contained in a dictionary not require updating the dictionary?

查看:111
本文介绍了为什么更改字典中包含的可变数组不需要更新字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一段来自CS193P课程(Objective-C)的代码。我注意到了编译器工作的方式。 NSMutableDictionary 已将 NSMutableArray code> photosByPhotographer 。稍后,更改照片,但未对 photosByPhotographer 进行任何更改。当我记录 photosByPhotographer 时,更改自动应用到它,它不需要任何额外的代码行!

I was trying a piece of code from CS193P course (Objective-C). I noticed something in the way that the compiler works. An NSMutableArray called photos was added to an NSMutableDictionary, photosByPhotographer. Later on, a change was made to photos without any changes to photosByPhotographer. When I logged photosByPhotographer, the change was automatically applied to it and it did not need any extra lines of code!

我不知道编译器如何使这项工作?任何要阅读的材料?

I wonder how the compiler makes this work? Any materials to read from?

代码如下:

- (void)updatePhotosByPhotographer
{
    NSMutableDictionary *photosByPhotographer = [NSMutableDictionary dictionary];
    for (NSDictionary *photo in self.photos) {
        NSString *photographer = [photo objectForKey:FLICKR_PHOTO_OWNER];
        NSMutableArray *photos = [photosByPhotographer objectForKey:photographer];
        if (!photos) {
            photos = [NSMutableArray array];
            [photosByPhotographer setObject:photos forKey:photographer];
            NSLog(@"photosByPhotographer in if: %@", photosByPhotographer);
        }
        [photos addObject:photo];
        NSLog(@"photosByPhotographer after if: %@", photosByPhotographer);
    }
    self.photosByPhotographer = photosByPhotographer;
}

NSLog()结果如下:

2012-07-20 20:05:57.618 Shutterbug[453:f803] photosByPhotographer in if: {
Dowbiggin =     (
);
}

2012-07-20 20:05:57.620 Shutterbug[453:f803] photosByPhotographer after if: {
Dowbiggin =     (
            {
        accuracy = 16;
        context = 0;
        dateupload = 1342836026;
        description =             {
            "_content" = "";
        };
        farm = 9;
        "geo_is_contact" = 0;
        "geo_is_family" = 0;
        "geo_is_friend" = 0;
        "geo_is_public" = 1;
        id = 7612787270;
        isfamily = 0;
        isfriend = 0;
        ispublic = 1;
        latitude = "37.307085";
        longitude = "-121.900395";
        originalformat = jpg;
        originalsecret = 052e70d412;
        owner = "22751315@N05";
        ownername = Dowbiggin;
        "place_id" = cils8sJUV7MeXHwt9A;
        secret = 4437007c99;
        server = 8161;
        tags = "square squareformat iphoneography instagramapp uploaded:by=instagram foursquare:venue=49f13597f964a5209c691fe3";
        title = "My little goofball";
        woeid = 55971033;
    }
);
}


推荐答案

正在使用对象并通过引用传递。

That's because in Cocoa, you are using objects and pass by reference.

想象下这段代码:

NSMutableArray *a, *b;

a  = [NSMutableArray new];
[a addObject:@"A"]; // a contains @"A"
b = a; // b is assigned to the same object
[b addObject:@"B"]; // a and b contain @"A", @"B"

NSLog(@"a = %p and b = %p", a, b); // same values

变量 a b 到同一对象。

The variables a and b point to the same object. You can also see that by comparing the pointer values.

但是,如果我们执行以下操作:

However, if we do the following:

NSMutableArray *a, *b;

a  = [NSMutableArray new];
[a addObject:@"A"]; // a contains @"A"
b = [[a mutableCopy] autorelease]; // b is assigned to a COPY of the object
[b addObject:@"B"]; // b contains @"A", @"B", while a still contains @"A"

NSLog(@"a = %p and b = %p", a, b); // NOT the same values

分配给 a (不是原始)的副本,因此 b 指向其他对象。您可以通过比较对象的地址进行检查。

b is assigned to a copy of a (not the original), so b points to an other object. You can check by comparing the addresses of the objects.

这篇关于为什么更改字典中包含的可变数组不需要更新字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆