I'm currently learning some objective C but still have problems with the syntax and creating objects.
The Situation: I need a two dimensional "personsArray" which contains many personArrays each containing a NSString *name and a NSNumber *amount (double). Finally I want to calculate some stuff with the array data in another view but I'm far apart from that..
My Plan: Creating a NSMutable Object when the program starts. If I click the Button "Add Person", it creates a personArray with two fixed values (later it should grab those of textAreas). Here is my Code:
Person.h
#import <Foundation/Foundation.h>
@interface Person : NSObject{
NSNumber *amount;
NSString *name;
}
- (void) createPersonArray:(double)theAmount withName:(NSString*) aName;
@end
Person.m
#import "Person.h"
@implementation Person
- (void) createPersonArray:(NSNumber*)theAmount withName:(NSString*) aName{
NSArray* personArray = [NSArray arrayWithObjects:theAmount,aName,nil];
}
@end
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak,nonatomic) IBOutlet NSMutableArray *personsArray;
- (IBAction)addPerson:(id)sender;
@end
ViewController.m
#import "ViewController.h"
#import "Person.h"
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)addPerson:(id)sender {
Person *newPerson;
newPerson = [Person new];
[newPerson createPersonArray:100.00 withName:@"test"];
[_personsArray addObject:newPerson];
}
@end
I know that this wont work, but I don't know how to realize the stuff that I described above. If I build the program, the app starts. if I click the button the debugger jumps to the createPersonArray and outputs "Thread 1: EXC_BAD_ACCESS (code=1,address=0x40590000] and "Unused variable 'personArray'".
I watched many tutorials know but none of them explained this situation.. How must my code be structured to accomplish my goal? Am I on the right path or is it the wrong approach?
greetings