Search code examples
iosobjective-cnsstringnsarraynsdictionary

How can I create this NSArray


I have a code to create an NSArray but it doesn't really do what I want.

This is the code:

NSString *workTime0 = [NSString stringWithFormat:@"6:00 - 16:00"];
NSArray *theBreaksArray = [NSArray arrayWithObjects:@"12:00 - 12:30", nil];

NSMutableArray *nameDictionary = [[NSMutableArray alloc] init] ;
                [nameDictionary addObject:workTime0];
                [nameDictionary addObject:theBreaksArray];

This gives me this:

(
   "6:00 - 16:00",
   (
      "12:00 - 12:30"
   )
)

But I want this:

Calendar = 
(
   "6:00 - 16:00",
   (
      "12:00 - 12:30"
   )
);

This is a picture of what I want:

enter image description here]

This is a picture of the full file:

enter image description here]

Thanks a lot!


Solution

  • You would probably want to create an NSMutableDictionary and add the array into it:

    NSMutableDictionary *calDictionary = [[NSMutableDictionary alloc] init];
                [calDictionary setObject:nameDictionary forKey:@"Calendar"];
    

    Result:

    {
        Calendar =     (
            "6:00 - 16:00",
                    (
                "12:00 - 12:30"
            )
        );
    }