Search code examples
iosobjective-cxcodensarraynsdictionary

Objective-C How to add objects to NSMutableArray from other place?


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!


Solution

  • 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.h
    //  Created by Don Mag on 8/30/20.
    
    #import <Foundation/Foundation.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface SomeClass : NSObject
    - (void)moreTabs:(NSMutableArray *)a;
    @end
    
    NS_ASSUME_NONNULL_END
    

    SomeClass.m

    //  SomeClass.m
    //  Created by Don Mag on 8/30/20.
    
    #import "SomeClass.h"
    
    @interface SomeClass()
    
    @end
    
    @implementation SomeClass
    
    - (void)moreTabs:(NSMutableArray *)a {
        [a addObject:@"B"];
        [a addObject:@"C"];
        [a addObject:@"D"];
        [a addObject:@"E"];
    }
    
    @end
    

    ExampleMenuViewController.h

    //  ExampleMenuViewController.h
    //  Created by Don Mag on 8/30/20.
    
    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface ExampleMenuViewController : UIViewController
    
    @end
    
    NS_ASSUME_NONNULL_END
    

    ExampleMenuViewController.m

    //  ExampleMenuViewController.m
    //  Created by Don Mag on 8/30/20.
    
    #import "ExampleMenuViewController.h"
    #import "SomeClass.h"
    
    @interface ExampleMenuViewController ()
    {
        NSMutableArray *vcTabs;
    }
    
    @end
    
    @implementation ExampleMenuViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        // add a button to the view
        //UIButton *b = [UIButton new];
        UIButton *b = [UIButton buttonWithType:UIButtonTypeSystem];
        [b setTitle:@"Tap Me" forState:UIControlStateNormal];
        b.frame = CGRectMake(0, 0, 200, 50);
        b.center = self.view.center;
        [self.view addSubview:b];
        [b addTarget:self action:@selector(btnTapped) forControlEvents:UIControlEventTouchUpInside];
        
        [self initVCTabs];
        [self logArray];
    }
    
    - (void)initVCTabs {
        // instantiate NSMutableArray
        vcTabs = [NSMutableArray new];
        // add one object
        [vcTabs addObject:@"A"];
    }
    - (void)btnTapped {
        SomeClass *sc = [SomeClass new];
        [sc moreTabs:vcTabs];
        [self logArray];
    }
    - (void)logArray {
        NSLog(@"vcTabs has %ld objects", [vcTabs count]);
        for (NSString *s in vcTabs) {
            NSLog(@"%@", s);
        }
    }
    
    @end
    

    When ExampleMenuViewController loads, it will add a button to the center of the view, then instantiate the vcTabs array and add one object - @"A".

    We log the array to the debug console and see:

    vcTabs has 1 objects
    A
    

    When you tap the button, an instance of SomeClass will be created, we call the moreTabs method in that class, passing a reference to vcTabs. That method will add 4 objects to the array - @"B" @"C" @"D" @"E".

    We then log the array to the debug console and see:

    vcTabs has 5 objects
    A
    B
    C
    D
    E