0

Unable to add NSmutableDictionary in NSmutableArray, activitiesFeedArray is a mutable array and initialized in header file.

            NSMutableDictionary *dummyitem = [[NSMutableDictionary alloc]init];
            NSMutableDictionary *dummyitem2 = [[NSMutableDictionary alloc]init];
            NSMutableDictionary *dummyitem3 = [[NSMutableDictionary alloc]init];

            [dummyitem setObject:@"No Data Found" forKey:@"text"];
            [dummyitem2 setValue:dummyitem forKey:@"Title"];
            [dummyitem3 setObject:dummyitem2 forKey:@"ItemInfo"];

            NSLog(@"%@",dummyitem3);

            //dummyitem3 logs correct value here             

            if ([activitiesFeedArray count] == 0) 
            { 
                NSLog(@"%@",dummyitem3);
                //dummyitem3 logs correct value here
                [activitiesFeedArray addObject:dummyitem3];
                NSLog(@"%@",activitiesFeedArray);
                //activitiesFeedArray logs null value here
            }

Viewdid load

    (void)viewDidLoad
{
   [super viewDidLoad];
   activitiesFeedArray = [[NSMutableArray alloc]init];
}

Header File

@interface SearchViewController : UIViewController <UISearchBarDelegate ,UITableViewDelegate , UITableViewDataSource>
{
  NSMutableArray *activitiesFeedArray;
}
@end
14
  • Where is activitiesFeedArray allocated? It doesn't look like you've initialized it anywhere. Commented Jan 20, 2012 at 5:00
  • 2
    "activitiesFeedArray is a mutable array and initialized in header file." How do you initialize in header file? It's not possible, so I'll say your problem is that activitiesFeedArray never get intialized Commented Jan 20, 2012 at 5:02
  • mind posting your viewDidLoad then? Commented Jan 20, 2012 at 5:16
  • Can you also show us the declaration of activitiesFeedArray in your header file? Commented Jan 20, 2012 at 5:40
  • 2
    Yeah that all looks fine too. Try adding descriptions into your NSLogs so that you know exactly what each is for: NSLog(@"dummyitem3 is %@",dummyitem3); NSLog(@"activitiesFeedArray is %@",activitiesFeedArray); Commented Jan 20, 2012 at 6:03

3 Answers 3

2

You don't mention where that code is, but it's clearly running some time before viewDidLoad, so the array is stil nil.

Sign up to request clarification or add additional context in comments.

1 Comment

@MattyG: Why on earth do you think that would cause an exception? [nil count] just returns 0 (just like every other message to nil).
1

If this is the only code you have, then it should work fine and NSLog of your NSMutableArray should not return nil. I copied and pasted your code on my XCode and it works perfectly. You must be doing something somewhere else in your code to alter NSMutableArray.


If you are curious what I did to prove that your code works, I created a simple UIViewController with one button in it. I then created a UIViewController class and literally copied your code in the header file and the implementation file. I declared the NSMutableArray in the header file and initialized it in viewdidload (I copied and pasted them as is). I also put the rest of your NSMutableDictionary code in the viewDidLoad and it produced sensible results. The NSMutableArray log shows that it contains the correct data.

Sometimes it helps to do a clean build but as far as I can tell you, your code is correct and should work.

By the way, I have IOS5 and XCode 4.2.

1 Comment

+1, In summary, the bug lies elsewhere. @ShahidAslam where else do you have code of the form "activitiesFeedArray = ..."?
1

Replace [dummyitem2 setValue:dummyitem forKey:@"Title"]; with [dummyitem2 setObject:dummyitem forKey:@"Title"];

1 Comment

Why would you suggest this? I don't see how it could make any difference.

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.