Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

fstigre

macrumors regular
Original poster
Aug 12, 2008
158
1
Hi,

I will start by saying what I need to acomplish. I have a button and a UITextfield, what I want to happen is...

1- Every time the button is clicked, whatever it is in the textfield to be put in an NSMutableArray (only numbers).
2- Display each item in the array to the screen (in a UILabel), each in a separate line.
3- Display the sum of all items in the array in a different UILabel.

My problem is getting the values from the textfield and appending them to the array.

The following code does exactly what I need BUT the values are NOT coming from a textfield, they are individual NSNumbers.

Code:
- (IBAction)addText:(id)sender { 
    
    NSNumber *myInt;
    myInt = [NSNumber numberWithInt:5];
    NSNumber *myInt2;
    myInt2 = [NSNumber numberWithInt:10];
    NSNumber *myInt3;
    myInt3 = [NSNumber numberWithInt:20];        
        
    NSMutableArray *numbers =  [NSMutableArray arrayWithObjects: myInt, myInt2, myInt3,nil];   
    
    int result =0;    
    NSString *string = @"";    
    
    for(int i = 0; i < [numbers count]; i++)    {
        
        int nums = [[numbers objectAtIndex:i] intValue];        
        self.display.numberOfLines=0;
        string = [string stringByAppendingFormat:@"Item: %i \n", nums];
        
        result += [[numbers objectAtIndex:i] intValue];
    }
        
    self.display.text = string;   
    self.total.text = [NSString stringWithFormat:@"Total: %i", result];    
    
}

I tried the following code but it only outputs whatever is in the textfield at the time of the execution, it doesn't retain previous values. I tried declaring the array and the textfield outside the method (addText) thinking that it was because of the life sycle of the method but it didn't work.

Code:
- (IBAction)addText:(id)sender { 
  
    NSNumber *myInt =[NSDecimalNumber decimalNumberWithString:self.inputText.text];       
    NSMutableArray *numbers =  [NSMutableArray arrayWithObjects: nil];
    
    [numbers addObject:myInt];  
    
    int result =0;       
    NSString *string = @"";    
    
    for(int i = 0; i < [numbers count]; i++)    {
        
        int nums = [[numbers objectAtIndex:i] intValue];        
        self.display.numberOfLines=0;
        string = [string stringByAppendingFormat:@"Item: %i \n", nums];
        
        result += [[numbers objectAtIndex:i] intValue];
    }
        
    self.display.text = string;   
    self.total.text = [NSString stringWithFormat:@"Total: %i", result];    
    
}

Any idea what am I doing wrong?


Thanks a lot.
 
Last edited:

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
I tried declaring the array and the textfield outside the method (addText) thinking that it was because of the life sycle of the method but it didn't work

Didn't work how? Did you get run-time or compile-time errors? More details please. Show your code for this as well.

Basically, your issue boils down to scope. You want your array to live longer than a single execution of a method. I think an instance variable in your viewController would work if you don't want your array to have scope outside of that viewController. If you do need it to have wider scope, you'll need consider other data-persistence approaches.
 

fstigre

macrumors regular
Original poster
Aug 12, 2008
158
1
Didn't work how? Did you get run-time or compile-time errors? More details please. Show your code for this as well.

Basically, your issue boils down to scope. You want your array to live longer than a single execution of a method. I think an instance variable in your viewController would work if you don't want your array to have scope outside of that viewController. If you do need it to have wider scope, you'll need consider other data-persistence approaches.

No, I didn't get any compile errors, it just didn't make a difference, it worked but it didn't retain previous values it only showed the last value.

I will play around with the scope of my array as you suggested, I just needed to know if I was on the right track which it looks like I'm, I just need to play around more.

Thanks a lot for your help.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Be sure your code is not running this line in addText:
Code:
NSMutableArray *numbers =  [NSMutableArray arrayWithObjects: nil];
Do you know why?
 

fstigre

macrumors regular
Original poster
Aug 12, 2008
158
1
No, can you please explain why?

and to be quite honest I didn't understand your statement either.

Be sure your code is not running this line in addText:
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Hmm, if you don't know what that line of code does, why do you have it in your code?

How comfortable are you with the fundamentals of Objective-C programming? If not much, I suggest you step away from the real coding and take the time to learn them before you return to tackle this issue.
 

fstigre

macrumors regular
Original poster
Aug 12, 2008
158
1
I didn't know what were you referring to with "addText", but I just realized that it is how I called my method. Sorry for the miss understanding, this is code I'm using for practicing purposes and I didn't put too much attention to how I named the method, my bad.

I now know exactly what you meant with that statement, sorry.
 

fstigre

macrumors regular
Original poster
Aug 12, 2008
158
1
I believe, it is because of the life cycle of the method, the array will basically die when the method finish its execution.

I hope I got that right.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
I believe, it is because of the life cycle of the method, the array will basically die when the method finish its execution.

If you make numbers a local variable within your addText: method, correct.

But, I'm asking about something else, to make sure you understand it correctly.
So, let's say you now have
NSMutableArray *numbers;
as an instance variable of your class.
Do you still want:
Code:
numbers =  [NSMutableArray arrayWithObjects:nil];
in your addText: method? If not, why not? If so, why so?
 

fstigre

macrumors regular
Original poster
Aug 12, 2008
158
1
This is what I did which worked.

Declared my NSMutableArray in my header file like so...

@interface ViewController : UIViewController
{
NSMutableArray *numbers;
}
...
@end

... then I defined it in the viewDidLoad method.

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
numbers = [NSMutableArray arrayWithObjects: nil];
}

but left ...
[numbers addObject:myInt];

in the addText method because this will add the numbers every time the method is called.

I hope this answers part of you questions.

Thanks a lot for trying hard to make me understand these basic and important programming concepts.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
I hope this answers part of you questions.

It does. Thanks.

Just a clarification, though. When you said you "defined it in the viewDidLoad", that's not exactly the case, at least to me. You defined/declared numbers in your header file. What you are doing in viewDidLoad is assign it a value, which happens to be an empty array. What you coded is equivalent to a common alloc-init, so I would word what you did there as "initializing" the value (which you need to do in order to add objects to it). And you could also have just initialized it with:
Code:
numbers = [[NSMutableArray alloc] init];
or even:

Code:
numbers = [NSMutableArray array];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.