0

I need to create a NSMutableArray with dictionaries like the one shown below..Donno how to do it ./

(
        {
        businessName = "IBM Business Continuity & Recovery Service";
        city = "Costa Mesa";
        phone = "(714) 668-6900";
        state = CA;
        street = "600 Anton Blvd";
        zip = 92626;
    },
        {
        businessName = "IBM Sanno";
        city = Lomita;
        phone = "(310) 626-0613";
        state = CA;
        street = "25835 Appian Way";
        zip = 90717;
    },
        {
        businessName = "Ewert's IBM Typewriter Service";
        city = "";
        phone = "(559) 732-3215";
        state = "";
        street = "";
        zip = "";
    },
)

Please guide me

1
  • 1
    There is a concept of NSDictionary which incorporates the various uses of an NSMutable array... Please google it for reference :) Commented Dec 10, 2012 at 6:14

5 Answers 5

3

NSMutableDictionary *list = [[NSMutableDictionary alloc] init];

[list setObject:businessName.text forKey:@"businessName"];
[list setObject:city.text forKey:@"city"];
[list setObject:phone.text forKey:@"phone"];
[list setObject:state.text forKey:@"state"];
[list setObject:street.text forKey:@"street"];
[list setObject:zip.text forKey:@"zip"];

NSMutableArray *listArraySignup = [[NSMutableArray alloc] init];
[listArraySignup addObject:list];
Sign up to request clarification or add additional context in comments.

Comments

2

If you want a NSMutableArray with dictionaries, you can do something like this

NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity: 10];

NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity: 10];

[dict setObject: @"IBM Business Continuity & Recovery Service" forKey: @"businessName"];

set other objects like this

[array addObject: dict];

[dict release];

Add the other dictionaries like this in the array and you will be good to go :)

1 Comment

@Divya this is the second time i'm editing an answer posted by you!strange
1

There are many ways to do this.

If you were planning on adding these dictionaries dynamically (roughly speaking):

NSMutableArray *dictionaries = [[NSMutableArray alloc] init];

NSDictionary *exampleDict = [[NSDictionary alloc] initWithObjectsAndKeys:
@"IBM Business Continuity & Recovery Service", @"businessName", 
@"Costa Mesa", @"city", 
@"(714) 668-6900", @"phone", 
@"CA", @"state", 
@"600 Anton Blvd", @"street", 
@"92626", @"zip", nil];

[dictionaries addObject:exampleDict];

Comments

1

with new Objective-C Literals

NSArray *array = @[
    @{
        @"businessName" : @"IBM Business Continuity & Recovery Service",
        @"city" : @"Costa Mesa",
        @"phone" : @"(714) 668-6900",
        @"state" : @"CA",
        @"street" : @"600 Anton Blvd",
        @"zip" : @(92626)
    },
    @{
        @"businessName" : @"IBM Sanno",
        @"city" : @"Lomita",
        @"phone" : @"(310) 626-0613",
        @"state" : @"CA",
        @"street" : @"25835 Appian Way",
        @"zip" : @(90717)
    },
    @{
        @"businessName" : @"Ewert's IBM Typewriter Service",
        @"phone" : @"(559) 732-3215"
    }
];

1 Comment

I'd prefer this method above John's approach. Probably because it's more readable
0

try this one, use NSDictionary

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.