0

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.

7
  • which error? maybe you didn't init the list? Commented Apr 8, 2013 at 3:23
  • **Incompatible pointer types assigning to 'NSMutableArray *__strong' from 'NSObjectClass *' ** Commented Apr 8, 2013 at 3:25
  • Maybe if you want to pass an NSMutableArray you should pass an NSMutableArray. Commented Apr 8, 2013 at 3:26
  • 1
    Note that you don't pass anything into a class, you pass stuff to an instance of a class. And the two most popular ways to do that are to make the thing being passed a parameter on init, or pass the value via a property. Commented Apr 8, 2013 at 3:29
  • 1
    Note that you should never name one of your classes "NS...". That prefix is reserved for Apple. Also avoid "UI", "CF", et al. Commented Apr 8, 2013 at 19:55

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

5 Comments

yup this is what I have done this bit I am not too sure on what to do is how to pass that returning MutableArray into a MutableArray in the Viewcontroller I call it from.. I hope that makes sense.
a little bit more clean code would be great.. what's this NSObjectClass?
@HurkNburkS did you implemented your variable like in my answer?
Sorry just been updating the question. It shows you I have implemented everything.
@HurkNburkS you can upload your .h & .m file to some file server & I will try it in my xcode by myself to solve it..

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.