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

ahan.tm

macrumors regular
Original poster
Jun 26, 2011
141
0
Florida
Hi,

I am trying to create an app in which I have an array(?) that holds the name and other information(2 strings, 3 int) of the user. I also want the user to specify how many names the app can hold.

I was wondering exactly how would I do this? I was wondering if I can use a 2-demisional array. Lastly, what I do to allow the array(?) to be saved.
 
Items placed in an NSArray must be objects, like NSString and NSNumber.

You cannot make 2 dimensional arrays, but because an NSArray is an object, they may be stored in each other. So you could have an NSArray of NSArrays.

To save an array, you'll likely want to use

Code:
[[NSUserDefaults standardsUserDefaults] setObject:myArray forKey:@"myArrayKey"];

To get the array back later, you can use

Code:
[[NSUserDefaults standardsUserDefaults] arrayForKey:@"myArrayKey"];
 
So. . .

I was looking in my objective-C book and it was talking about NSMutableArrays.

So, I would have an NSMutableArray for the name, one for the other string, and 3 more for the ints, and I would have the array sorted out so the name is with the corrosponding ints and strings?

Thanks
 
Ok,

After a lot of research, I found out that I can use an NSArray filled with an NSDictionary for each user's information. Can someone please explain how this would work and how I would do this?

Thanks:)
 
Break It Down until you get steps you know how to do.

Top-level steps:
1. Fill one dictionary with one user's information.
2. Add the filled dictionary to an NSArray.
3. Repeat as needed.

Do you know how to do step 1? Step 2? If not, then look at the reference docs for the class.

If doing both steps is too hard to figure out, then just do step 1: Fill one dictionary with one user's information. Of course, to figure that out, you'll have to break it down again:

1) Decide how to store each value:
Strings can be stored as NSString, but what about ints?
You can't store ints directly in an NSDictionary, so what class should you use to hold an int value?

2) Decide on a name (key) for each value.
To be stored in a plist, keys must be strings (see plist programming guide link below).


If you've never used any of the classes before, then you might want to try a simpler app first. One of the biggest mistakes beginners make is picking an app to write because they want to use it, but with no idea of how to begin writing it. The way to begin is to Break It Down.

You should at least look at the sample code associated with the class reference doc for NSArray, NSDictionary, and the mutable subclasses of each. Start at Collections Programming Guide and see the class reference docs at the bottom of the left-hand column. Each reference doc has links to sample code.

Also be sure to read the Property List Programming Guide.

You will also have to decide on what strings to use as keys for filling the dictionary. Since you haven't said anything about what information you'll be storing for each user, no one can suggest meaningful keys. For the user name, however, I suggest @"username" for that key.
 
Last edited:
Hi,

Thanks for your replies. I have had experience with NSArray before and now I am beginning to understand how to all works.

So, I would create a class, and that class would be the blueprint for all the NSDictionaries that would go into a NSArray.

Example: User taps + button in tableview, app goes to class, copies it into the array as a dictionary and the user fills out the information. Is this correct?

Thanks
 
Can you please explain how I would do that?
Break It Down:
1. Write your custom class (use @interface & @implementation).
2. Make an instance of your custom class (use alloc & init).
3. Add the instance to an array (use NSMutableArray's addObject:).
4. Repeat as needed.

If you need further explanation, please identify exactly which step needs to be explained.
 
Okay,

I am breaking it down and will post here as I work on it.

So far, my code is such
Code:
@interface Child : NSObject{
    NSString *name;
    NSString *prize;
    int marblesneeded;
    int colormarbles;
    
}

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *prize;

@end

This is in a new Obj-C class file and have
Code:
#import "child.h"
in my ViewController

Is this correct? I will keep on following tutorials and will post back as I pass each step with questions.

I was also wondering, how could I integrate this with a tableview with a plus button on the top?
 
Code:
#import "child.h"

What exactly is the filename of the .h file? If it's client.h then #import "client.h". If it's actually Client.h (with a capital C) then #import "Client.h".

Chances are either will work in practice, but that's only because the case-insensitive Mac OS X filesystem is covering up the error.
 
Whoops!

I just typed that there, it is correct in Xcode. Is everything else correct before I continue?
 
I am having trouble implementing the class.

How would I create a tableview with a plus button, and when someone adds a person, they can setup the name, prize, marblesneeded and the color marbles? Then, when an action occurs for a specific person, it adds 1 to marblesneeded?

Also, how would I use Page Control to switch between the persons without changing the view?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.