NSMutableDictionary and a button to send changes

Discussion in 'iOS Programming' started by Carll, Jan 10, 2013.

  1. Carll, Jan 10, 2013
    Last edited by a moderator: Jan 10, 2013

    Carll macrumors newbie

    Joined:
    Jan 10, 2013
    #1
    Hi, I'm new here and I'm pretty noob (as you can see) at programming, I've only a bit of experience with Javascript so I've some troubles looking at objc and xcode for the first time... that's why I would like to ask you some help :)

    To be short I'm trying to make a simple app which gets data (a name and a value) from a UITextField and UIlabel.

    I have a "sender" button too, i would like to use the sender button to set the NSMutableDictionary, my first idea was to set a key and as value an object (an array of numbers), in this code you won't find out any array for now, i just want to make it works the dictionary first

    the app is working well (except for the dictionary) and i know you'll find a lot of mistakes so advise me without any mercy, i'll be glad to learn

    here is the code, i've colored the part of interest:


    ViewController.h


    Code:
    
    @interface ViewController : UIViewController {
    
    IBOutlet UIView *votePage;
    IBOutlet UIView *archivePage;
    [COLOR="orange"]NSMutableDictionary *mutableDictionary;
    [/COLOR]
    
    }
    
    -(IBAction) archivePage;
    -(IBAction) votePage;
    
    [COLOR="Orange"]@property (retain, nonatomic) NSMutableDictionary *mutableDictionary;
    [/COLOR]@property (retain, nonatomic) IBOutlet UILabel *myLabel;
    @property (retain, nonatomic) IBOutlet UITextField *myTextView;
    @property (retain, nonatomic) IBOutlet UILabel *myVote;
    
    @end
    


    ViewController.m

    Code:
    
    @interface ViewContorller()
    @end
    
    @implementation ViewController
    
    @synthesize mutableDictionary;
    
    
    -(IBAction)votePage: (id)sender {
    
    self.view = votePage;
    
    }
    
    -(IBAction)archivePage: (id)sender {
    
    self.view = archivePage;
    
    }
    
    -(IBAction)myTextView: (id)sender {
    }
    
    
    -(IBAction)sendVote: (id)sender {
    [self.myTextView resignFirstResponder];
    self.myLabel.text = self.myTextView.text;
    [COLOR="orange"][mutableDictionary setValue:self.myVote.text forKey:self.myTextView.text];
    [/COLOR]
    }
    
    
    -(IBAction)addVote: (UIStepper*)sender {
    
    NSUInteger value = sender.value;
    self.myVote.text = [NSString stringWithFormat:@"%d",value];
    
    }
    
    -(void)viewDidLoad
    {
    [super viewDidLoad];
    self.view = votePage;
    
    }
    
    -(void)didReceiveMemoryWarning
    {
    [super didReceiveMemoryWarning];
    }
    
    -(void) dealloc {
    [_myLabel release];
    [_myTextView release];
    [_myVote release];
    [super dealloc];
    
    }
    
    @end
    


    main.m

    Code:
    int main(int argc, char *argv[])
    {
        @autoreleasepool {
            return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    
       [COLOR="orange"] NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"10", @"First Collection", nil];
    
    NSLog(@"mutableDictionary = %@", mutableDictionary);[/COLOR]
    
    }
    
    i've added a picture that show both pages of what i'm trying to do, in the first page you'll see the structure of how to write and send values, in the second (which is clean) i wanted to store my list from the NSMutableDictionary (but before i need it to work eheh)

    [​IMG]

    of course i'm missing something important but since I can't figure out what looking in the web i've decided to try and ask you any help :) hope you want to help me, thanks a lot in advance

    Carll
     
  2. ArtOfWarfare, Jan 10, 2013
    Last edited: Jan 10, 2013

    ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    You should almost never modify main.m. I would delete everything you added there, first.

    Next... What makes you think your NSMutableDictionary isn't working? I'd suggest clicking in the left margin of the method that modifies the dictionary to set a break point and stepping through to see the changes that take place during your method.

    To use the debugger to step through your code:
    1 - set a break point by clicking in the left margin. It should draw a blue tag/arrow.
    2 - run your app in the simulator.
    3 - when it reaches the breakpoint, it should pause execution of your program and open a panel at the bottom of Xcode's window with the console. In the top right of the panel are tabs to show/hide the console and the values in your program. Pick a tab that lists the values.
     
  3. Carll thread starter macrumors newbie

    Joined:
    Jan 10, 2013
    #3
    yes thank you, i've removed everything from the main and I did a NSLog into the sender button that returns the value associated with the key, it works !

    but seems like he's not yet able to Store value/keys in the mutabledictionary when i write in the textview...

    what I'm still missing? perhaps the method i've used to set value and key is wrong?

    thanks for the breakpoint explanation, im going to test =)
     
  4. KoolStar macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #4
    According to your code, you never allocate or create the mutable dictionary. Therefore when you go to add your key/value pair it is not added to anything.

    In your viewDidLoad you need to add:

    Code:
    self.mutableDictionary = [NSMutableDictionary new];
    
    The new keyword is a shortcut for [[NSMutableDictionary alloc] init]. Remember this if you are using a non arc project you will have to release the resource in the dealloc.
     
  5. Carll, Jan 10, 2013
    Last edited by a moderator: Jan 10, 2013

    Carll thread starter macrumors newbie

    Joined:
    Jan 10, 2013
    #5
    thank you, i've did what you said and after your advises i've tried with a nsdictionary with some values and keys and it works properly, i still believe my mistake is when i try to set value/key on the nsmutabledictionary, in fact (probably this doesn't matters but im going to tell you :D) if i write in the sender button:

    Code:
    [mutableDictionary setObject:self.myVote.text objectForKey:self.myTextView.text];
    i've got a warning "instance method '-setObject: objectForKey' not found (return type defaults on 'id')

    the old code was:

    Code:
    [mutableDictionary [COLOR="Orange"]setValue[/COLOR]:self.myVote.text [COLOR="orange"]forKey[/COLOR]:self.myTextView.text];
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Does NSMutableDictionary have a setObject: objectForKey: instance method?
     
  7. Carll thread starter macrumors newbie

    Joined:
    Jan 10, 2013
    #7
    thank you for editing my posts with "code" :) however no, you're right, but the same warning appears to me if I use the setObject: forKey: or setValue: forKey: methods
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Then are you certain you're calling either of those methods on an NSMutableDictionary? You should verify its type.
     
  9. KoolStar macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #9
    This is the only logical last step. I just created the same sample application roughly and its working just fine. Im not getting that error. What SDK version are you using and what XCode version?
     
  10. Carll, Jan 10, 2013
    Last edited: Jan 10, 2013

    Carll thread starter macrumors newbie

    Joined:
    Jan 10, 2013
    #10
    to be honest I'm not sure :p that's why I have troubles, the declaration of my NSMutableDictionary is correct or should I specify some type or anything else?

    xcode 4.5.2
     
  11. KoolStar macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #11
    hmmm, if you want you could post a zip of the code you have and I can compile and have a look see whats going on.
     
  12. Carll, Jan 10, 2013
    Last edited: Jan 12, 2013

    Carll thread starter macrumors newbie

    Joined:
    Jan 10, 2013
    #12
    ok I will, I sent you a pm asking about your working code to see what I misunderstood

    however I will upload my code in a while in this post

    here is the zip with the xcode project, please don't laugh at my mistakes^^ i'm new at programming and i would like to understand this part as soon as possible to play with data structures and make more complicated app

    so thank you very much for any help you gave to me :)
     
  13. Carll thread starter macrumors newbie

    Joined:
    Jan 10, 2013
    #13
    just to tell you, it works well now and i'm done, im' just practicing with more dictionries and arrays thanks for everything :p
     

Share This Page