How to add a variable to array in Objective-C?

I have a program that I am making, which has two arrays, one for usernames and one for passwords. I am trying to add a string that a user inputs into a textbox to an array. Instead, the program is only adding the string once. How do I fix this?

I am using an NSMutableArray. My code is:

    NSString *usrname = [NSString stringWithFormat:@"%d", usrString];
[usrs addObject:usrname];

This code adds the string once, but when I change the string and press the button again, it doesn't add the second string.


Check in your code that by mistake you did not set array with fixed capacity by ([[NSMutableArray alloc] initWithCapacity: arrayCapacity]);

You just need to init without capacity ( NSMutableArray *array = [[NSMutableArray alloc] init]; ) so that it will take it upto available memory.

Hi Based on the info which you provided you have to write the below code in .h and .m files In .h file

IBOutlet UITextFiled *userNameTextField,*PasswordTextField;
IBOutlet UIButton *loginButton;
NSMutableArray *usrs,*passwords;


In .m file


usrs=[[NSMutableArray alloc]init];
passwords=[[NSmutableArray alloc]init];



NSString *userstr=[[NSString alloc]initWithFormat:@"%@",userNameTextField.text];
NSString *passwordstr=[[NSString alloc]initWithFormat:@"%@",PasswordTextField.text];

[usrs addObject:userstr];
[passwords addobject:passwordstr];


I hope this will helpful to you. Now the data will add into the array but if you close your app or if you move from login page to another page the array will get clear.In order to store the data in permanent manner you have to use plist or sqlite.

Need Your Help

SQL Server 2005/2008: Identify current user

sql-server sql-server-2005 tsql sql-server-2008 user-management

I have a web application, which is using a SQL Server 2005 database.