Reputation: 185
** Could somebody help me to understand this type of initialization? It seems to me that this part of code:
"username: @"Johny"
look like nsdictionary initializationa object for key?**
NSArray *items = @[@{@"username": @"Johny",
@"userpic": @"Photo.png",
@"image": @"photo1.jpg"},
@{@"username": @"George",
@"userpic": @"Photo.png",
@"image": @"photo2.jpg"},
@{@"username": @"Mandy",
@"userpic": @"Photo.png",
@"image": @"photo3.jpg"},
@{@"username": @"Jacob",
@"userpic": @"Photo.png",
@"image": @"photo4.jpg"},
@{@"username": @"Brandon",
@"userpic": @"Photo.png",
@"image": @"photo5.jpg"},
@{@"username": @"Dave",
@"userpic": @"Photo.png",
@"image": @"photo6.jpg"}
];
*in my code i'm getting all values by using for loop *
for (NSDictionary *dictionary in items) {
{
//
}
Upvotes: 1
Views: 293
Reputation: 167
There were some changes introduced to Objective-C by Apple at the 2012 WWDC. If you have not seen the WWDC 2012 video Modern Objective-C
then I would highly recommend you viewing that video which explains the changes introduced. Amongst those changes added were Array Literals
and Dictionary Literals
Basically just like you can create a String Literal
by creating the following:
NSString *name = @"Slim Shady"
Apple introduced Array Literals
and Dictionary Literals
too Following examples are from the Video
Where initially to create an Array the options were:
NSArray *myArray;
myArray = [NSArray array]; // an empty Array
myArray = [NSArray arrayWithObject:anObject]; // an array with a single object
myArray = [NSArray arrayWithObjects: a, b, n, nil]; // array with 3 objects a, b, c
Array Literals allows you to create an Array in the following ways:
myArray = @[ ]; // an empty Array
myArray = @[anObject]; // array with a single object
myArray = @[a, b, c]; // array with 3 objects a, b, c
as you can see using the Literals it is clearer and easier to create the Array. Similarly for NSDictionary
Where initially the options to create a Dictionary were:
NSDictionary *myDict;
myDict = [NSDictionary dictionary]; // empty Dcitionary
myDict = [NSDictionary dictionaryWithObject:object forKey:key];
myDict = [NSDictionary dictionaryWithObjectsAndKeys: object1, key1, object2, key2, nil];
Dictionary Literals allow you to create the dictionary in the following ways:
myDict = @{ }; // empty ditionary
myDict = @{ key:object }; // notice the order of the key first -> key : object
myDict = @{ key1:object1 , key2:object2 };
Upvotes: 0
Reputation: 122401
It's an array of dictionary objects using the new(ish) Objective-C literals syntax.
As well as the traditional literal strings, which we all know and love: @"Hello World"
, there are:
NSArray
literals: @[ element1, element2 ]
, which has the advantage of not needing a trailing nil
like [NSArray arrayWithObjects:]
does.NSDictionary
literals: @{ key : value, key : value }
, which has the advantage of being more natural in the key-value order compared to [NSDictionary dictionaryWithObjects:forKeys:]
.NSNumber
literals: @(YES)
(boolean), @(1.2)
(floating point), @(123)
(integer).and they all have the advantage of being much more concise and natural.
Upvotes: 2
Reputation: 1472
In iOS 6, Apple created a new way of initialising NSArray
and that's what is the case here.
It functions just like the arrayWithObjects
functions, just that the syntax is a little different.
Your NSArray
is filled with NSDictionary
objects.
Upvotes: 0