1

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]'

8
  • Please put your code into code tags (4 spaces indention) Commented Dec 10, 2015 at 12:54
  • @AminNegm-Awad Thanks for your response. but i cant get you.. Commented Dec 10, 2015 at 12:55
  • user3201089 or Surya, please read the SO documentation. Code citation should be formatted as code. Commented Dec 10, 2015 at 12:57
  • Are you sure that menu method is called after loadData finishes? Commented Dec 10, 2015 at 12:58
  • 1
    @Avi please guide me.. how to add array value to menu???? Commented Dec 10, 2015 at 13:07

1 Answer 1

1

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.

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

7 Comments

Thanks for your response.. but i got nil value.Random Selection is:(null)
Yes, like I said - in your code, you create strArray in loadData. If menu method is called before loadData, then strArray is bound to be nil. Ideally, you'd create your menu, load the data from web and then refresh the menu.
my issue fix. but no items present in the menu
@VijaySanghavi - there is a link to the github page with it in the question, no need to clutter it.
@user3201089 - again, that is because strArray is created and populated after creating the menu.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.