Handling Strings And Ints

Discussion in 'iOS Programming' started by ahan.tm, Sep 20, 2011.

  1. ahan.tm macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #1
    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.
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    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"];
     
  3. ahan.tm thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #3
    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
     
  4. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4
    Your problem is fundamentally the same as the problem being discussed in this thread.
     
  5. ahan.tm thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #5
    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:)
     
  6. chown33, Sep 27, 2011
    Last edited: Sep 27, 2011

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    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.
     
  7. thedollarhunter macrumors member

    Joined:
    May 9, 2011
    Location:
    UK
    #7
  8. ahan.tm thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #8
    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
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Why not just have instances of that class (objects) go into the array?
     
  10. ahan.tm thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #10
    Can you please explain how I would do that?

    Thanks
     
  11. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #11
    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.
     
  12. ahan.tm thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #12
    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?
     
  13. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    No, it's not. Remember that Objective-C is case-sensitive.
     
  14. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #14
    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.
     
  15. ahan.tm thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #15
    Whoops!

    I just typed that there, it is correct in Xcode. Is everything else correct before I continue?
     
  16. ahan.tm thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #16
    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?
     

Share This Page