Basic Objective C class to return an array

objective-c

Solution

The call would looke like this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    ReturnClass *retClass = [[ReturnClass alloc] init];
    NSMutableArray *latLngArray = [retClass getLocation];
}

This would give you the array produced by `getLocation`.

This code makes a new instance of `ReturnClass` every time it needs the array; this may or may not be what you want, because you cannot keep any state between invocations. If keeping state in `ReturnClass` is what you need, you would have to implement a singleton (here is a link to a question explaining a very good way of implementing singletons in Objective C).

Problem

I am new to Objective C and am trying out some basic concepts but hitting a few brick walls Essentially what I am trying to do is write a class that returns an array when messaged from another class The class that returns the value would be something like - ReturnClass.h ``` #import <Foundation/Foundation.h> @interface ReturnClass : NSObject { } -(NSMutableArray *) getLocation ; @end ``` ReturnClass.m ``` #import "ReturnClass.h" @implementation ReturnClass -(NSMutableArray *) getLocation { //do some stuff to populate array //return it return latLngArray; } @end ``` I then wanted to call this in to another implementation in order to harvest the value SomeOtherClass.m ``` #import "ViewController.h" #import "ReturnClass.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSMutableArray *latLngArray = [[getLocation alloc] init]; } ``` I appreciate there is probably a fair bit wrong with this but please be gentle as I am trying to learn :)

Original source

Related problems