Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 0 down vote accepted

I think if you want save array to MutableArray, you only need create method in ViewController. If you create Person.h, i suggest you should save to MutableArray is Person Object. You can use code below:

Person.h:

@property (nonatomic, strong) NSNumber *amount;
@property (nonatomic, strong)  NSString *name;

instead:

NSNumber *amount;
NSString *name;

Person.m:

@implementation Person
@synthesize name;
@synthesize amount;

- (void) createPersonArray:(double)theAmount withName:(NSString*) aName{
    amount = [NSNumber numberWithDouble:theAmount];
    name = aName;
}

ViewController.h

@property (strong,nonatomic) NSMutableArray *personsArray;

ViewController.m

- (void)viewDidLoad
{

    [super viewDidLoad];
    _personsArray = [[NSMutableArray alloc]init];

}

- (IBAction)addPerson:(id)sender {

    Person *newPerson;
    newPerson = [Person new];
    [newPerson createPersonArray:100.00 withName:@"test"];

    [_personsArray addObject:newPerson];

}

If you don't understand. You can comment below, i will help you.

When you need get contain, you can use:

for (Person *person in _personsArray){
    NSLog(@"Amount: %@ \n Name: %@ \n ",ps.amount,ps.name,);
}

or

for (int i = 0; i< [_personsArray count]; i++){
    Person *ps = [_personsArray objectAtIndex:i];

    NSLog(@"Amount: %@ - Name:%@",ps.amount,ps.name);
}

================================== UPDATE ===========================

double sumAmount = 0;

for (int i = 0; i < [_personsArray count]; i++){
    Person *person = [_personsArray objectAtIndex:i];
    sumAmount = sumAmount + [person.amount doubleValue];
}

double averageAmount = sumAmount / [_personsArray count];

NSLog(@"%f",averageAmount);
share|improve this answer
    
I understand your solution. So you we are just creating a mutable array of many Person objects? How do I reach the content of the different Persons? I already printed the objects into NSLog: here is the code in fiddle: jsfiddle.net/reneil/7pEbr –  r3n Jun 3 at 8:16
    
You can view my edit :) –  hoptqVN.dev Jun 3 at 8:58
    
I changed Person.h, Person.m and ViewController.m. –  hoptqVN.dev Jun 3 at 9:01
    
works great thank you! –  r3n Jun 3 at 11:59
    
I'm trying to sum up the values of all amounts, but cannot find the right syntax for it: jsfiddle.net/reneil/ZqUF4 :/ –  r3n Jun 3 at 13:27

First, personsArray variable is not a outlet. So, remove outlet property and replace weak with strong:

@property (strong,nonatomic) NSMutableArray *personsArray;

Second, need to initialize NSMutableArray before using it, maybe in viewDidLoad:

- (void)viewDidLoad
{
   [super viewDidLoad];
   _personsArray = [NSMutableArray new];
}    
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.