0

I am looking to get an NSString value from a Text Field and add it to an array, I want to build an array with many strings in it ex: [hello, goodbye, too soon].

This is my current solution:

- (IBAction)submitButton:(id)sender {


NSMutableArray *wordArray = [[NSMutableArray alloc] init];
NSString *input = textField.text;
[wordArray insertObject:input atIndex:arrayIndex];
arrayIndex++;
}

This works for the first item in the array, but when I press submit again it reinitializes.My issue is how do I initialize the NSMutableArray to use in the button function, without having it in there so that it doesn't initialize every time. Thank you

3
  • initialize it in viewDidLoad and then inside your function use self.wordArray. viewDidLoad is called only once for your viewController. Commented Nov 29, 2016 at 22:23
  • Ok so I initialized the array in the viewDidLoad now how do I use self with the array in the button function? - (void)viewDidLoad { [super viewDidLoad]; NSMutableArray *wordArray = [[NSMutableArray alloc] init]; } Commented Nov 29, 2016 at 22:49
  • Declare it as a property:@property(weak) NSMutableArray * wordArray; then initialize it in viewDidload using wordArray = [[NSMutableArray alloc] init];. Don't declare it inside viewDidLoad itself. ( sorry using my iPhone...) Commented Nov 29, 2016 at 23:13

3 Answers 3

2

Your are using a local array that disappears as soon as the submitButton method is finished.

Make your wordArray an instance variable and initialize it once in viewDidLoad. Then in your submitButton: method (and any others), you reference the instance variable instead of creating local arrays.

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

3 Comments

So I have initialized my array in viewDidLoad using NSMutableArray = NSMutableArray *wordArray = [[NSMutableArray alloc] init]; and in my button function I used [self.wordArray insertObject:input atIndex:arrayIndex]; it is saying in the viewDidLoad that the wordArray is unused and in the self.wordArray line it gives an error Bad receiver type void. In the header file, I am declaring my wordArray like so: @interface ViewController : UIViewController <UITextFieldDelegate> { NSMutableArray *wordArray; NSUInteger arrayIndex; } the text field and button are also declared
you need to initialise it in viewDidLoad, but not declare it there. It needs to be declared in class scope as an instance variable or property.
@Luckabo, your updated code is wrong also. I spelled it out the solution my answer.
0

Honey's answer is almost, but not, correct.

Your code uses a local variable in your submitButton method, and creates a new, empty array each time the method gets called. Both of those things are wrong.

Honey's answer has you create a different local variable in viewDidLoad. That's also wrong.

You need to make wordArray an instance variable or property of your class. If you class is called ViewController, say, it might look like this

@interface ViewController: UIViewController;

@property (nonatomic, strong) NSMutableArray *wordArray

...

@end

And then initialize it in viewDidLoad:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.wordArray = [[NSMutableArray alloc] init];
}

Then in the rest of your program refer to self.wordArray, the property.

3 Comments

It is a poor recommendation to put the wordArray property in the public interface of the .h file. It should be a private property. It should be declared in the private class extension in the .m file.
@rmaddy If you are working on a framework that will be shared with other developers, or on a large project that is divided into modules, and you have a public and a private interface for those modules then I agree. For somebody who doesn't understand the difference between a local variable and an instance variable, however, I think you're raising "learn to fly before you can crawl" issues.
Yeah but this is how bad habits start. Show people the proper way to code their solutions.
0

Here's the solution,

@implementation ViewController{
   NSMutableArray *_wordArray;
}
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    _wordArray = [[NSMutableArray alloc] init];
}

- (IBAction)submitButton:(id)sender {
    NSString *input = textField.text;
    [wordArray addObject:input];
}

You was re init the array each time you make the action, which will let you always save the last value of the textfield. but this creates an array as global variable so that you can add all the values entered in textfield.

Hope this help you :)

1 Comment

While this is a valid solution, don't just post a bunch of code with no explanation followed by "hope it helps". A good answer explains what was wrong and how the answer solves the problem. Code-only answers are frowned upon on stack overflow.

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.