Gianluca Carpi
Gianluca Carpi

Reputation: 81

Array with array inside

I need to create an array with personal name inside,and on this name,another array with personal information.Basically an array with another array inside divided by name.

Es:Array[luca [born:x age:y lives:z] marco[born:x age:y lives:z]......}

How can i do that?

Upvotes: 0

Views: 82

Answers (3)

Michael
Michael

Reputation: 6899

To add onto these answers you can also easily create a mutable NSMutableDictionary or NSMutableArray using literal syntax as follows:

NSMutableDictionary *dict = [@{@"asdf":@"asdf"} mutableCopy];

NSMutableArray *arr = [@[@"asdf"] mutableCopy];

Upvotes: 0

Earl Grey
Earl Grey

Reputation: 7466

Very simple, use modern Objective-C literals.

NSDictionary *luca = @{@"name" : @"luca",
                       @"born" : @(1997),
                       @"lives" : @(5)};

NSDictionary *marc = @{@"name" : @"marc",
                       @"born" : @(1998),
                       @"lives" : @(2)};


NSArray *people = @[luca, marc];

Upvotes: 1

Ian MacDonald
Ian MacDonald

Reputation: 14010

Your brief description indicates that you might want a dictionary of dictionaries. However, what you want here is an array of dictionaries or an array of objects.

var people = [ ["name": "Luca", "born": x, "age": y, "lives": z],
                ... ]

or

struct Person {
  var name:String
  var born:Int16
  var age:Int16
  var lives:Int16
}

var array = [Person(name: "Luca", born: x, age: y, lives: z),
             ...]  

Upvotes: 1

Related Questions