3

tell me please, how do I create an array in which each element will have a number of properties. For example:

array:
  |
  |-item 1 ( property_1-"Name1", property_2-"LastName1", property_3-"Age1");
  |-item 2 ( property_1-"Name2", property_2-"LastName2", property_3-"Age2");
  |-item 3 ( property_1-"Name3", property_2-"LastName1", property_3-"Age2");
  |-…

In this case, the different elements of an array can have one and the same property, such as in the code posted above - "item 3" has the "property 2" is the same as in "item 1", and "property 3" is the same as in "item 2"

Tell me, please, how best to do it and, if not difficult, write a simple example or a link to some tutorial.

Thank you in advance)

2
  • Are the properties same? Or have some patterns? property_1 has the pattern that has same prefix 'Name' and the number increase one bye one. But LastName... and Age... does not have. So what do you want? Commented Nov 25, 2012 at 10:47
  • @sunkehappy - Each element always has all these properties, if you are about it Commented Nov 25, 2012 at 10:50

2 Answers 2

2

There are two methods that I would suggest:

1. Use a class to store all the properties

Here's an example:

@interface Wrapper : NSObject

@property (nonatomic, assign) NSString* property_1;
@property (nonatomic, assign) NSString* property_2;
@property (nonatomic, assign) NSString* property_3;

@end

Then you can use it as a dictionary:

NSString* value=[myWrapperInstance valueForKey: @"property_1"];

But here comes the alternative solution:

1. Use a NSDictionary to store all the properties

NSDictionary* dict= @{ @"property_1" : @"Name1" ,@"property_2" : @"Name2",  @"property_3" : @"Name3" };

Then the solution comes easy:

NSMutableArray* objects=[NSMutableArray new];
for(int i=0; i<N; i++)
{
    NSDictionary* dict= @{ @"property_1" : @"Name1" ,@"property_2" : @"Name2",  @"property_3" : @"Name3" };
    [objects addObject: dict];
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for solution #1 - almost always better to use object-oriented solution. To editorialize a bit - I see dozens of questions per week here on SO where the asker is going through convoluted hoops to use NSArray and NSDictionary to manage his entire model layer only to wonder later why it seems so difficult to manage.
1

Sounds like you want an NSArray of NSDictionary objects:

NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:@"Name1", @"property_1", @"LastName1", @"property_2", @"Age1", @"property_3", nil);
NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:@"Name2", @"property_1", @"LastName2", @"property_2", @"Age2", @"property_3", nil);
NSDictionary *dict3 = [NSDictionary dictionaryWithObjectsAndKeys:@"Name3", @"property_1", @"LastName3", @"property_2", @"Age3", @"property_3", nil);

NSArray *array = [NSArray arrayWithObjects:dict1, dict2, dict3, nil];

If you want to update this later then you should use NSMutableDictionary and NSMutableArray, respectively.

If you are not using ARC, the you need to release objects when you are finished with them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.