Search code examples
iosobjective-cnsarraynsdictionary

Create a new NsmutableArray in ios


Hello I am newbie to ios and objective c. I am working on a demo. In that I am having a for loop for adding values to NSMutableArray. Now this array will be added to NSMutableDictionary. Now inside this for loop i am having an if condition for checking string. I am adding objects to the array in "if" part and want to create a new Array when condition fail. I have tried as below, but in my Dictionary only one array saved. Can anyone help me where i have made mistake?

NSMutableArray *listingCommonArr,sectionTitles,eventDate;

int j=0;
NSString *dt;
NSMutableArray *sectionArray = [[NSMutableArray alloc]init];
sectionTitles = [orderedSet array];

    NSLog(@"===listing common array---%d",[listingCommonArr count]);

    sectionData = [[NSMutableDictionary alloc]init];
    for(int i =0;i<[listingCommonArr count];i++)
    {

         dt = [[listingCommonArr objectAtIndex:i] objectForKey:@"start_date"];

        NSLog(@"====my date at place===%@ & my date is====%@",[sectionTitles objectAtIndex:j],dt);
        for (int i =0; i < [listingCommonArr count]; i++) {
            dt = [[listingCommonArr objectAtIndex:i] objectForKey:@"start_date"];

            if ([(NSString *)[sectionTitles objectAtIndex:j] isEqualToString:dt]) {
                [sectionArray addObject:listingCommonArr[i]];
                //sectionData = @{dt : sectionArray}; // <-- PAY ATTENTION ON THIS LINE, PLEASE

                //[sectionData setValue:sectionArray forKey:dt];

                sectionData = [@{dt : sectionArray} mutableCopy];

            } else {
                [sectionData setObject:@"New value" forKey:@"string"];
                sectionArray = [[NSMutableArray alloc]init];
                j++;
            }
        }
    NSLog(@"====my section DAta is==%@",sectionData);
    }

errorTrace

-[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance 0x7fc6547832a0
2015-12-18 19:39:41.387 iPhoneExploreCayman[41719:13822557] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance 0x7fc6547832a0'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010a551c65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010a1e8bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010a5590ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010a4af13c ___forwarding___ + 988
    4   CoreFoundation                      0x000000010a4aecd8 _CF_forwarding_prep_0 + 120
    5   iPhoneExploreCayman                 0x000000010604a48c -[iPhoneECListingView GetEventListingData:] + 1884
    6   iPhoneExploreCayman                 0x000000010605644b -[iPhoneECListingView EventListStart] + 1323
    7   iPhoneExploreCayman                 0x0000000106055a98 -[iPhoneECListingView EventClicked:] + 2312
    8   UIKit                               0x0000000108832d62 -[UIApplication sendAction:to:from:forEvent:] + 75
    9   UIKit                               0x000000010894450a -[UIControl _sendActionsForEvents:withEvent:] + 467
    10  UIKit                               0x00000001089438d9 -[UIControl touchesEnded:withEvent:] + 522
    11  UIKit                               0x000000010887f958 -[UIWindow _sendTouchesForEvent:] + 735
    12  UIKit                               0x0000000108880282 -[UIWindow sendEvent:] + 682
    13  UIKit                               0x0000000108846541 -[UIApplication sendEvent:] + 246

Solution

  • I'm highly confused about what you actually want to do here, but... after your comments I have completely updated my answer removing the previous assumptions about what actually is supposed to happen.

    it seems to me you want(ed) to group a series of words by the first letter. you have posted a list of animals as example, so I worked with those.

    you defined the expected output:

    NSDictionary *_expectedOutput = @{@"A" :@[@"Affrican cat", @"Assian cat", @"Alsesian fox"], @"B" : @[@"Bear", @"Black Swan", @"Buffalo"], @"C" : @[@"Camel", @"Cockatoo"], @"D" : @[@"Dog", @"Donkey"], @"E" : @[@"Emu"], @"R" : @[@"Rhinoceros"], @"S" : @[@"Seagull"], @"T" : @[@"Tasmania Devil"], @"W" : @[@"Whale", @"Whale Shark", @"Wombat"]};
    

    as you can see my input array is just an unsorted list of random names of animals from your sample output:

    NSArray *_input = @[@"Whale Shark", @"Tasmania Devil", @"Affrican cat", @"Bear", @"Seagull", @"Black Swan", @"Whale", @"Wombat", @"Camel", @"Rhinoceros", @"Cockatoo", @"Buffalo", @"Assian cat", @"Alsesian fox", @"Emu"];
    

    I'm working with that input array and I am going to group them by their first letter and build up the output:

    NSMutableDictionary *_output = [NSMutableDictionary dictionary];
    [[_input sortedArrayUsingSelector:@selector(compare:)] enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *_firstLetter = (obj.length > 0 ? [obj substringToIndex:1] : nil);
        if (_firstLetter) {
            NSMutableArray *_list = [_output valueForKey:_firstLetter];
            if (_list == nil) {
                _list = [NSMutableArray arrayWithObject:obj];
                [_output setValue:_list forKey:_firstLetter];
            } else {
                [_list addObject:obj];
            }
        }
    }];
    

    that is pretty much it, if you'd log the _output, you can see on the console that is identical to your _expectedOutput collection.