Dmitriy Babinskiy Dmitriy Babinskiy - 3 months ago 10
iOS Question

How to work with one array in two methods

How can i insert value in array in one method and work with this full of value array in other method.

-(void)firstMethod {

NSMutableArray *array = [[NSMutableArray alloc] init];
../// add some value in array.
}

-(void)secondMethod {


..// here i want to work with array which consist of value from first method.

}

Answer

You can create one instance of NSMutableArray and use that in the both method.

@interface ViewController () {
    NSMutableArray *array;
}
@end

Now access this array in both methods

-(void)firstMethod {

    array = [[NSMutableArray alloc] init];
    [array addObject:@"Hello"];
}

-(void)secondMethod {
    if (array) {
        [array addObject:@"World"];//Add object that you want
    }
    else {
        array = [[NSMutableArray alloc] init];
        [array addObject:@"World"];
    }
}
Comments