Hi everyone
During my studies about iOS (Stanford course) I came across following code and it got me thinking about a lot of things. Here's the code in FlickrHelper's implementation:
Btw: places looks like this (with many more elements in the dictionary).
Here are the questions:
1 Why is a dictionary being returned? Couldn't it be an NSArray. Because an NSArray could also be a collection of dictionaries. Or wouldn't it be possible to do placesByCountry[country] as country is a string?
2
If this element doesn't exist will it return nil or crash?
3 PlacesOfCountry is contained within the for-loop. So PlacesOfCountry doesn't set to nil as long we're in the for-loop (or does it go to nil with each iteration of the for-loop?). Am I correct assuming that by repeating NSMutable Array *placesOfCountry the following happens:
A say placesByCountry[country] contains 4, the pointer refers to memory place starting at 4
B say placesByCountry[country] contains 896, the pointer refers to memory place starting at 896
C say placesByCountry[country] contains nothing, the pointer is set at nil and is giving a pointer value by the alloc init in the if-clause.
In this case the same variable name placesOfCountry would possibly be shifting to different memory places in each iteration of the for-loop.
4 Why would you go saying then
. Wouldn't it be better to shift the declaration before the for-loop starts?
tx !
During my studies about iOS (Stanford course) I came across following code and it got me thinking about a lot of things. Here's the code in FlickrHelper's implementation:
Code:
+ (NSDictionary *)placesByCountry:(NSArray *)places
{
NSMutableDictionary *placesByCountry = [NSMutableDictionary dictionary];
for (NSDictionary *place in places) {
NSString *country = [FlickrHelper countryOfPlace:place];
NSMutableArray *placesOfCountry = placesByCountry[country];
if (!placesOfCountry) {
placesOfCountry = [NSMutableArray array];
placesByCountry[country] = placesOfCountry;
}
[placesOfCountry addObject:place];
}
return placesByCountry;
}
+ (NSString *)countryOfPlace:(NSDictionary *)place
{
return [[[place valueForKeyPath:FLICKR_PLACE_NAME]
componentsSeparatedByString:@", "] lastObject];
}
Btw: places looks like this (with many more elements in the dictionary).
Code:
places: (
{
"_content" = "Bom Jesus da Lapa, Bahia, Brazil";
latitude = "-13.249";
longitude = "-43.409";
"photo_count" = 274;
"place_id" = YWVVOUBVV7ya3PE;
"place_type" = locality;
"place_type_id" = 7;
"place_url" = "/Brazil/Bahia/Bom+Jesus+da+Lapa";
timezone = "America/Bahia";
"woe_name" = "Bom Jesus da Lapa";
woeid = 456084;
},
{
"_content" = "\U0141\U00f3d\U017a, Lodz, Poland";
latitude = "51.761";
longitude = "19.468";
"photo_count" = 90;
"place_id" = Yg5Az8ZUUr8ITa4;
"place_type" = locality;
"place_type_id" = 7;
"place_url" = "/Poland/Lodz/%C5%81%C3%B3d%C5%BA";
timezone = "Europe/Warsaw";
"woe_name" = "\U0141\U00f3d\U017a";
woeid = 505120;
}}
Here are the questions:
1 Why is a dictionary being returned? Couldn't it be an NSArray. Because an NSArray could also be a collection of dictionaries. Or wouldn't it be possible to do placesByCountry[country] as country is a string?
2
Code:
NSMutableArray *placesOfCountry = placesByCountry[country];
3 PlacesOfCountry is contained within the for-loop. So PlacesOfCountry doesn't set to nil as long we're in the for-loop (or does it go to nil with each iteration of the for-loop?). Am I correct assuming that by repeating NSMutable Array *placesOfCountry the following happens:
A say placesByCountry[country] contains 4, the pointer refers to memory place starting at 4
B say placesByCountry[country] contains 896, the pointer refers to memory place starting at 896
C say placesByCountry[country] contains nothing, the pointer is set at nil and is giving a pointer value by the alloc init in the if-clause.
In this case the same variable name placesOfCountry would possibly be shifting to different memory places in each iteration of the for-loop.
4 Why would you go saying then
Code:
NSMutableArray *placesOfCountry = placesByCountry[country];
tx !
Last edited by a moderator: