HurkNburkS
HurkNburkS

Reputation: 5510

How to return a NSMutableArray from a method

I have a method in a NSObject Class that I am executing from another ViewController Class, I would like to reutrn a NSMutableArray however I am not sure how to pass that into a variable in the ViewController class.

Updated Code:

NSObject Class is called Axis

Axis.h // declare my method here with return type of NSMutableArray

- (NSMutableArray *)assignAxes:(NSArray*)axesData;

Axis.m

- (NSMutableArray *)assignAxes:(NSArray*)axesData {
//..

//pass some NSDictionaries into a MutableArray called myMutableArray

return myMutableArray;
}

Now I have a View Controller called FinalViewViewController and I want to call assignAxes method from this viewController, and I would like to put the returning myMutableArray into a mutableArray variable in FinalViewViewController but am not sure how... I only know how to call the method not pass the returning value into a variable to be used.

FinalViewViewController.m

Axis *axis = [[Axis alloc] init];
    NSMutableArray *tempGetSeriesObjArray = [[NSMutableArray alloc] init]; // create holder variable for returning mutableArray
    tempGetSeriesObjArray = [axis assignAxes:series]; // gives an error

This is the error I am getting from that last line of code

Incompatible pointer types assigning to 'NSMutableArray *__strong' from 'Axis *'

any help would be appreciated.

Upvotes: 0

Views: 1672

Answers (1)

In your NSObject Class, first go in your .h file and make the declaration of the array and set the property

@interface YourNSObjectClass : NSObject {
    NSMutableArray *_myArray;
}

@property (nonatomic, retain) NSMutableArray *_myArray;

- (NSMutableArray *) getMyArray; // declare method

@end

So now, you have to sync your variable and initialize it in the initMethod - if you have a custom init-method you have to declare it too and make it inside of there.

@synthesize _myArray;

- (id) init {
     if(self = [super init]) {
           _myArray = [[NSMutableArray alloc] init];
     }
     return self;
}

And the getter-method should work too

- (NSMutableArray *) getMyArray {
    return _myArray;
}

Upvotes: 1

Related Questions