Hello I'm junior in Objective-C and Swift programming.
I have NSMutableArray in ExampleMenuViewController.m or(and) SomeClass.m declared as vcTabs.
NSMutableArray *vcTabs;
When I have two declares 'vcTabs' Xcode returns duplicate symbol '_vcTabs' (...)
How to add objects to an existing NSMutableArray init in other class (ExampleMenuViewController.m)? I need append new objects from another class (SomeClass.m) to vcTabs (NSMutableArray).
I wrote in SomeClass.m this code:
if ([Tools isNonullValueForKey:[dictionary valueForKey:@"additional_tabs"]]) {
additional_tabs = [dictionary valueForKey:@"additional_tabs"];
NSLog(@"additionalTabs count: %lu", [additional_tabs count]);
for (int i = 0; i < [additional_tabs count]; i++) {
if ([Tools isNonullValueForKey:[additional_tabs valueForKey:@"_id"]]) {
additional_tab_id = [[additional_tabs valueForKey:@"_id"] objectAtIndex:i];
}
if ([Tools isNonullValueForKey:[additional_tabs valueForKey:@"names"]]) {
NSDictionary *dic = [[additional_tabs valueForKey:@"names"] objectAtIndex:i];
_en_additional_tab_name = [dic valueForKey:@"en"];
_pl_additional_tab_name = [dic valueForKey:@"pl"];
}
if ([Tools isNonullValueForKey:[additional_tabs valueForKey:@"url"]]) {
additional_tab_url = [[additional_tabs valueForKey:@"url"] objectAtIndex:i];
//NSLog(@"additional_tab_url: %@", _additional_tab_url);
}
[vcTabs addObject:[[VCTab alloc] initWithIdAndTypeAndUrl:additional_tab_id :VCTabAdditional :additional_tab_url]];
NSLog(@"%@ %d %@ %@ %@ %@", @"pos", i, @"id: ", additional_tab_id, @"url: ", additional_tab_url);
}
}
ExampleMenuViewController method with initVCTabs
- (void)initVCTabs {
vcTabs = [[NSMutableArray alloc] init];
[vcTabs removeAllObjects];
if ([Tools getBooleanUserDefault:@"example_visible_tab_attendees" :YES]) {
[vcTabs addObject:[[VCTab alloc] initWithType:VCTabAttendees]];
}
(...)
if ([Tools getBooleanUserDefault:@"example_visible_tab_user_info" :YES]) {
[vcTabs addObject:[[VCTab alloc] initWithType:VCTabUserInfo]];
}
if ([Tools getStringUserDefault:@"example_additional_tab_id" :@""]) {
NSString *additionalTabId = [Tools getStringUserDefault:@"conference_additional_tab_id" :@""];
NSString *additionalTabUrl = [Tools getStringUserDefault:@"conference_additional_tab_url" :@""];
NSLog(@"additionalTabId %@", additionalTabId);
NSLog(@"additionalTabUrl %@", additionalTabUrl);
[vcTabs addObject:[[VCTab alloc] initWithIdAndTypeAndUrl:additionalTabId :VCTabAdditional :additionalTabUrl]];
}
}
PS. If I use from ExampleMenuViewController I have only one tab with last object properties... but additional_tabs
array have 17 objects.
Do you have any ideas or advices? All the best for you everyone!
When are you calling
initVCTabs
?When / how is the code in
SomeClass.m
running?For being a "junior in Objective-C and Swift programming" you seem to have a lot going on that you don't understand yet. Try creating a new project and learn how things work -- then implement that in your full project.
Here is a very, very simple example. With the information you provided in your question, this may or may not relate directly - but it should give you an idea of where to go:
SomeClass.h
SomeClass.m
ExampleMenuViewController.h
ExampleMenuViewController.m
When
ExampleMenuViewController
loads, it will add a button to the center of the view, then instantiate thevcTabs
array and add one object -@"A"
.We log the array to the debug console and see:
When you tap the button, an instance of
SomeClass
will be created, we call themoreTabs
method in that class, passing a reference tovcTabs
. That method will add 4 objects to the array -@"B" @"C" @"D" @"E"
.We then log the array to the debug console and see: