Reputation:
I am trying to create a dynamic top menu. I have to get some data from a json request and display this data in one of the sections of the top menu. I'm new in Objective-C. I also tried with NSMutableArrays and I had an error. Only one MutableArray and I can show the top menu. I am following this third party framework for top menu “https://github.com/dopcn/DOPNavbarMenu”.
- (DOPNavbarMenu *)menu {
if (_menu == nil) {
[strArray objectAtIndex:0];
NSLog(@"Random Selection is:%@",strArray);
_menu = [[DOPNavbarMenu alloc] initWithItems:@[strArray] width:self.view.dop_width maximumNumberInRow:_numberOfItemsInRow];
_menu.backgroundColor = [UIColor blackColor];
_menu.separatarColor = [UIColor whiteColor];
_menu.delegate = self;
}
return _menu;
}
-(void)loadData
{
strResponse=[dictionary objectForKey:@"data"];
strMsg=[strResponse valueForKey:@"Text"];
NSLog(@“string message is :%@",strMsg);
NSLog(@"String Response is :%@",strResponse);
NSLog(@"Text Response is: %@",strMsg);
strArray = [[NSMutableArray alloc] init];
[strArray addObject:strMsg];
NSLog(@"Array values are - %@", strArray);
}
Array values are: Life Style,Care Plans,Trackers/Diaries,Questionnaires/Assessments.
but i got exception like this:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'
Upvotes: 1
Views: 4922
Reputation: 8006
Ok, per your comment
but i want to call menu method before loadData finishes
the issue lies here :
_menu = [[DOPNavbarMenu alloc] initWithItems:@[strArray] width:self.view.dop_width maximumNumberInRow:_numberOfItemsInRow];
Before loadData
is run, I assume that strArray
is nil
. This causes this part @[strArray]
to fail - this creates a new array with strArray
as its only element, which cannot be nil
.
I also assume that you wanted to rather pass strArray
itself there, not wrap it in another array.
Now, if you call menu
before populating strArray
in loadData
, there will likely be no items present in the menu, unless you have a way to update it with new items after loadData
finishes.
To summarise : to fix your immediate issue, you should change the above line to this :
_menu = [[DOPNavbarMenu alloc] initWithItems:strArray width:self.view.dop_width maximumNumberInRow:_numberOfItemsInRow];
which should work, but there won't be any items present in the menu, because of reasons explained above.
Upvotes: 1