我在尝试CS193P(Objective-C)课程的一段代码时注意到编译器的工作方式。一个名为photos的NSMutableArray被添加到了一个NSMutableDictionary中,该字典叫做photosByPhotographer。稍后,对于photos进行了更改,而没有对photosByPhotographer进行任何更改。当我输出photosByPhotographer时,更改自动应用到它上面,不需要任何额外的代码!
我想知道编译器是如何实现这个功能的?有什么相关的材料可以阅读吗?
以下是代码:
我想知道编译器是如何实现这个功能的?有什么相关的材料可以阅读吗?
以下是代码:
- (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;
}
);
}