0

In my app i use

[theMutableArray addObject:theString];

several times and it works every time, however when trying to add in another view it won't add.

I know my string is not null because i have

NSLog(@"%@, %@", theString, theMutableArray);

and it returns "the string value", (null)

In my viewDidLoad i have

theMutableArray = [[NSMutableArray alloc] init];

and where i try to add it I have

theString = [alertView textFieldAtIndex:0].text;
[theMutableArray addObject:theString];
NSLog(@"%@, %@", theString, theMutableArray);

Is there a typo of any sort? or did i forget something?

5
  • You can not add anything to array if it is not created (= nil). Commented Apr 19, 2014 at 17:01
  • 1
    It is deallocated at the end of the method. I think you mean to make your array an @property so that your class has a reference to it and you can access beyond the method where you initialized it. Commented Apr 19, 2014 at 17:02
  • @JoePasq Tried @property(nonatomic, strong) for my mutable array, theres no difference Commented Apr 19, 2014 at 17:10
  • have you been accessing it with self.theMutableArray? Commented Apr 19, 2014 at 17:13
  • no, the only ways I've used it were in the header file, and what i have posted Commented Apr 19, 2014 at 17:14

3 Answers 3

1

You said that

NSLog(@"%@, %@", theString, theMutableArray);

returns "the string value", (null). So what do you expect? Your theMutableArray seems to be nil and this is a reason why you can not add anything to it.

Sign up to request clarification or add additional context in comments.

Comments

1

Make this a property of your class so that you can access it across the lifetime of its parent object and not just within one method. (I presume you want to access it in multiple methods).

@interface myClass : NSObject
@property (nonatomic) NSMutableArray *theMutableArray;
@end

@implementation myClass

- (void) viewDidLoad {
    self.theMutableArray = [NSMutableArray new];
}

- (IBAction) anActionDidHappen:(id) sender {
    …
    theString = [alertView textFieldAtIndex:0].text;

    [self.theMutableArray addObject:theString];
}

@end

Comments

0

I added these 3 lines and it worked, i probably has something wrong with the array somewhere else.

 NSMutableArray *newMutableArray = [[NSMutableArray alloc] init];
[newMutableArray addObject:theString];

theMutableArray = newMutableArray;

4 Comments

Is this in your header (.h) file?
No , it's in a void method after getting the value of the text field input from an alert view
Okay. I can't help but ask where theMutableArray is initialized and if it is static or an @property?
The only initialization i am currently using is the theMutableArray = [[NSMutableArray alloc] init];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.