1

I'm working in objective C on xCode and I'm having a hard time solving an issue with mutable arrays. I have the following method in my main view controller .m file

- (IBAction)addData:(UIButton *)sender {    
double x = [_xField.text doubleValue];
double y = [_yField.text doubleValue];
[inputXData addObject:[NSNumber numberWithDouble:x]];
[inputYData addObject:[NSNumber numberWithDouble:y]];
}

which takes the input fields xField and yField and adds their input values to arrays inputXData and inputYData.

I'd like to declare inputXData and inputYData as an NSMutableArray so that they're accessible to this method (and so I can add to them or remove from them at will eventually), but I know initializing them in this method every time it runs would make no sense.

Where should I initialize these arrays? Should I write special methods to initialize them? If so, what should I name them and where should I put them?

Also, I'd like inputXData and inputYData to be available in other views later. Will I need extra work to make sure I keep these arrays between views?

  • You should find a good book or a series of online tutorials. Have a look at [Good resources for learning ObjC](http://stackoverflow.com/q/1374660). The Big Nerd Ranch books are excellent, and lots of people like the Stanford iOS course on iTunes U. Good luck! – jscs May 27 '15 at 18:53
  • Read about init methods in Objective C. And while you are at it familiarize yourself with view controller methods such as viewDidLoad. Also read about MVC (model view controller). For now you can do it in the init, later it may need to move to a model class. – Gruntcakes May 27 '15 at 19:00

0 Answers0