1

so i have a multi-dimensional NSMutableArray. The first Array holds info such as username, password, first name, last name and email address.

NSMutableArray *newUser = [[NSMutableArray alloc]init];
[newUser addObject:username];
[newUser addObject:password];
[newUser addObject:firstName];
[newUser addObject:lastName];
[newUser addObject:emailAddress];
int localUserID = 0;

This array is then added to an array with all other users.

NSMutableArray *userArray = [[NSMutableArray alloc]init]; // app crash occurs on this line.
[userArray insertObject: newUser atIndex:localUserID];
localUserID++;

so my basic question is, why does the NSMutableArray not like to work with the

atIndex:

line but it works fine if i use

[userArray addObject:newUser];

I would simply user the later line, but when i add a new user it overlaps the last. Any help is much appreciated.

4
  • NSMutableArray is representing user? Why you don't create subclass of NSObject called User with properties username, password, firstname, lastname, emailAddress and localUserID? Commented Dec 12, 2013 at 22:22
  • I do have that. This array's purpose is to save these to NSUserDefaults Commented Dec 12, 2013 at 22:24
  • Paste here log with crash. You can use simplier syntax for initializing new object - [NSMutableArray new]. Commented Dec 12, 2013 at 22:24
  • Then you should implement the NSCoding protocol in your user class, and serialize to data. Or use JSON and add JSON encoding/decoding to your user class. Commented Dec 12, 2013 at 22:35

3 Answers 3

3

You're using [NSMutableArray insertObject:atIndex:] incorrectly. From the docs:

This value [index] must not be greater than the count of elements in the array.

So if you have an empty mutable array, you can only insert at position 0. This method doesn't automatically resize the array for you.

If you're only going to have values for some indices, an NSMutableDictionary may be a much better fit for your task. The keys would be the indices, and the values would be your objects. No need to resize an array, and risk introducing bugs.

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

2 Comments

so should i add an object and then put my object in this array?
Well, if you must have your object at position x, you may want to pre-populate your array (you can't insert nils but you can insert [NSNull null]). But you should really consider a dictionary that would map integers (indices) to objects. That seems like a much cleaner way to do what you want.
1

I think that since the array is empty in when you create it, you can not use [NSMutableArray insertObject:atIndex:], because it needs an already existing index.

Comments

1

Why are you using an array of arrays? I think creating a User class or using NSDictionary would be better.

With dictionary:

NSMutableDictionary * newUser = [ NSMutableDictionary dictionary ] ;
newUser[@"username"] = <user name>;
newUser[@"first name"] = <first name>;

[ userArray addObject:newUser ] ;

Or create a User class:

@interface User : NSObject
@property ( nonatomic, copy ) NSString * username ;
@property ( nonatomic, copy ) NSString * firstName ;
...
@end

@implementation User
@end

Use like:

User * user = [ [ User alloc ] init ] ;
user.username = <user name> ;
user.firstName = <first name> ;
...

[ userArray addObject:user ] ;

Also, DO NOT store any passwords in plain text in your app or app preferences--you need to use Keychain Services for that

Comments

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.