Variables in Arrays in Objectives-C

Discussion in 'iOS Programming' started by eatpie26, Jan 4, 2011.

  1. eatpie26, Jan 4, 2011
    Last edited by a moderator: Jan 4, 2011

    eatpie26 macrumors newbie

    Joined:
    Jan 4, 2011
    #1
    I am trying to make an app that stores multiple strings in an array. I have an NSMutableArray and a string. I am trying to add the string to the array. I have already alloc/init the array, and it isn't working. My code is: [usrs addObject:usrname]. Any ideas?
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    "It isn't working" is awfully vague. Please elaborate. What isn't working? Are you getting compile-time errors or warnings? Or run-time errors? Anything in the console? Is the app crashing? If so, what does the crash log say? Etc. The more details you can provide us, the easier it will be to try to help you out.
     
  3. eatpie26 thread starter macrumors newbie

    Joined:
    Jan 4, 2011
    #3
    Sorry. Like I said, I'm new to this. My app compiles fine, but when I press the button that is supposed to trigger this code, it doesn't add the string.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Are you new to debugging as well? Like, what kind of things would you want to test to verify that your code is working as hoped?
     
  5. eatpie26, Jan 4, 2011
    Last edited by a moderator: Jan 4, 2011

    eatpie26 thread starter macrumors newbie

    Joined:
    Jan 4, 2011
    #5
    I am setting a variable to the number of items in the array, and getting the program print out the number of items. I am doing this using the following code:

    Code:
         		NSString *string;
    		int count = [usrs count];
    		string = [NSString stringWithFormat:@"%d", count];
    		label.text = string;
     
  6. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #6
    And what output does that give you? What do you expect? How are you building the array in the first place?
     
  7. zachsilvey macrumors 6502

    Joined:
    Feb 5, 2008
    Location:
    Battle Ground
    #7
    Did you connect the button in IB?
    Did you remember to save IB?

    Those are usually my mistakes.
     
  8. eatpie26 thread starter macrumors newbie

    Joined:
    Jan 4, 2011
    #8
    I have connected the button in IB. The output that it gives is the same every time that I press the button. I'm not sure whether the problem is with the integer or with the array. I assumed the problem was with the array, but I'm probably wrong. Any ideas to see if the problem is with the integer or the array?
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    I would say it's time to learn how to use the debugger, set up breakpoints, step through code and examine variable values.
     
  10. hayesk macrumors 65816

    Joined:
    May 20, 2003
    #10
    Why don't you add a couple of NSLog statements in your method, and then check the run console?
     
  11. eatpie26 thread starter macrumors newbie

    Joined:
    Jan 4, 2011
    #11
    Do you know of a good place that I can learn?
     
  12. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
  13. eatpie26 thread starter macrumors newbie

    Joined:
    Jan 4, 2011
  14. eatpie26, Jan 5, 2011
    Last edited by a moderator: Jan 5, 2011

    eatpie26 thread starter macrumors newbie

    Joined:
    Jan 4, 2011
    #14
    I have figured out the problem. Either the label is not refreshing itself or the variable is not refreshing itself. My guess is that the problem is with the variable. Any suggestions as to how to get the variable to set itself to the number of items in the array every time I press the button? Once again, my code is:

    Code:
         if ([usrString length] != 0 && [passwdString length] != 0){
    		NSString *string;
    		int count = [usrs count];
    		string = [NSString stringWithFormat:@"%d", count];
    		label.text = string;
         }
     
  15. dejo, Jan 5, 2011
    Last edited: Jan 5, 2011

    dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #15
    Glad you're making progress but debugging really isn't a matter of guessing what the problem is; it's a process of determining exactly what is going wrong. I'd suggest setting a breakpoint on the line that defined and sets count. Then, use the debugger to check what values are being assigned to your various variables as you step through the code. This should help you determine if the issue lies with the variable or the label.

    Also, which variable are we taking about? Your code snippet references at least 4.
     
  16. eatpie26 thread starter macrumors newbie

    Joined:
    Jan 4, 2011
    #16
    Could you please tell me how to use the debugger to check the values of the variables?
     
  17. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #17
    Wirelessly posted (Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5)

    The section called Viewing Variables and Memory in the docs I linked to earlier might be a good place to start. ;)

    B
     
  18. chown33, Jan 5, 2011
    Last edited: Jan 5, 2011

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #18
    Post the code that actually adds a string to the 'usrs' array. Show the entire method, not just the isolated fragment you first posted.

    When you run the code you've posted, what is the count that's shown? Is it zero? If so, then you may have a completely different problem, which is that the usrs array is nil.


    I'll repeat this from another thread:
    You need to describe two things:
    1. What you expected to happen.
    2. What actually happened.

    We know what you expect to happen: you expect the shown count to increase as each string is added. What we don't know is what actually happened: you haven't said what the shown count actually is.

    You also haven't yet posted a complete method that is called as the button-action that adds a string to the array. It's difficult to debug what's happening when you click the button, because you haven't shown all the code that runs when you click the button. Posting isolated fragments only works when the problem is in the isolated fragment.


    If I had to guess why the posted fragment isn't causing the shown count to change, my guess is that usrString or passwdString is nil, or one of their lengths is zero. In short, my guess is that it's doing exactly what you told it to do, but you've neglected something somewhere else.

    I see nothing obviously wrong in the posted code, so my guess is the problem is in the code you haven't posted, which runs at some time before the posted code. Post that code.
     
  19. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #19
    When members who are trying to help link you to important documents in your quest (I'm talking about balamw here), it would probably behoove you to actually read them over before posting further questions that seem to indicate that you have not taken advantage of their good advice.
     
  20. eatpie26, Jan 5, 2011
    Last edited by a moderator: Jan 5, 2011

    eatpie26 thread starter macrumors newbie

    Joined:
    Jan 4, 2011
    #20
    When I run this code and it comes to the end, I have it run the code that I posted earlier. Ideally, what would happen is my program would find out how many items are in the array, and print it through a label. It is doing this after I add the first item, and it prints 1. Then, the label continues to say 1 no matter how many times I press it. Here is the rest of the code that is supposed to run when you press the button:


    Code:
    	self.usrString = usr.text;
    	self.passwdString = passwd.text;
    	
    	usrs = [[NSMutableArray alloc] init];
    	if ([usrString length] != 0 && [passwdString length] != 0) {
    		NSString *string1;
    		int count = [usrs count];
    		string1 = [NSString stringWithFormat:@"%d", count];
    		label1.text = string1;
    		NSString *usrname = [NSString stringWithFormat:@"%u", usrString];
    		[usrs addObject:usrname];
    	}else if ([usrString length] == 0 && [passwdString length] == 0) {
    		label.text = @"You must enter a valid username and password.";
    	}else if ([usrString length] == 0) {
    		label.text = @"You must enter a valid username.";
    	}else if ([passwdString length] == 0) {
    		label.text = @"You must enter a valid password.";
    	}	
    	
    	
    	/*for (u = 0; u < count; u++) {
    		<#statements#>
    	}*/
    	
    	if ([usrString length] != 0 && [passwdString length] != 0){
    		NSString *string;
    		int count = [usrs count];
    		string = [NSString stringWithFormat:@"%d", count];
    		label.text = string;
    	}
    	}
    
    Any ideas?
     
  21. chown33, Jan 5, 2011
    Last edited: Jan 5, 2011

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #21
    Look carefully at the red-hilited line of code, and ask yourself this question:
    Do you really want to do that every time you click the button?

    If you don't know the answer to that question, ask yourself this:
    What happens to usrs when you assign it a new array every time the button is clicked?

    And you still haven't posted the complete action-method for the button click. I suggest doing so.

    Finally, you should read this sticky, then make sure all code you post has CODE tags around it:
    http://forums.macrumors.com/showthread.php?t=747660
     
  22. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #22
    Further hint. Is the single value in the array the first one you added or the most recent one? You could tell if you used one of the various methods to view the content of your variables.

    This is one really easy way to see what is in your array mentioned earlier in the thread.

    B
     
  23. eatpie26, Jan 5, 2011
    Last edited by a moderator: Jan 5, 2011

    eatpie26 thread starter macrumors newbie

    Joined:
    Jan 4, 2011
    #23
    How would I make it so that the program only alloc/init it once? Also, I'm not sure what else there is to post from my code, so I'll just post my entire MyViewController.m files:


    Code:
    //
    //  MyViewController.m
    //
    //
    
    #import "MyViewController.h"
    #import "usrAppDelegate.h";
    
    @implementation MyViewController
    @synthesize usr;
    @synthesize passwd;
    @synthesize usrString;
    @synthesize passwdString;
    @synthesize label;
    @synthesize label1;
    @synthesize usrs;
    //@synthesize string;
    - (IBAction)newUsr:(id)sender {
    	self.usrString = usr.text;
    	self.passwdString = passwd.text;
    	
    	usrs = [[NSMutableArray alloc] init];
    	int u;
    	if ([usrString length] != 0 && [passwdString length] != 0) {
    		NSString *string1;
    		int count = [usrs count];
    		string1 = [NSString stringWithFormat:@"%d", count];
    		label1.text = string1;
    		NSString *usrname = [NSString stringWithFormat:@"%u", usrString];
    		[usrs addObject:usrname];
    	}else if ([usrString length] == 0 && [passwdString length] == 0) {
    		label.text = @"You must enter a valid username and password.";
    	}else if ([usrString length] == 0) {
    		label.text = @"You must enter a valid username.";
    	}else if ([passwdString length] == 0) {
    		label.text = @"You must enter a valid password.";
    	}	
    	
    	
    	/*for (u = 0; u < count; u++) {
    		<#statements#>
    	}*/
    	
    	if ([usrString length] != 0 && [passwdString length] != 0){
    		NSString *string;
    		int count = [usrs count];
    		string = [NSString stringWithFormat:@"%d", count];
    		label.text = string;
    	}
    	}
    
    
    - (IBAction)existUsr:(id)sender {
    	self.usrString = usr.text;
    	self.passwdString = passwd.text;
    	usrs = [[NSMutableArray alloc] init];
    	[usrs addObject:@"F"];
    	int count = [usrs count];
    	NSString *string = [NSString stringWithFormat:@"%d", count];
    	label.text = string;
    }
    
    - (BOOL)textFieldShouldReturn:(UITextField *)theTextField{
    	if (theTextField == usr){
    		[usr resignFirstResponder];
    	}
    	if (theTextField == passwd) {
    		[passwd resignFirstResponder];
    	}
    	return YES;
    }
    /*
     // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
        if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
            // Custom initialization
        }
        return self;
    }
    */
    
    /*
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad {
        [super viewDidLoad];
    }
    */
    
    /*
    // Override to allow orientations other than the default portrait orientation.
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    */
    
    - (void)didReceiveMemoryWarning {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
        
        // Release any cached data, images, etc that aren't in use.
    }
    
    - (void)viewDidUnload {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    
    
    - (void)dealloc {
    	[usr release];
    	[label release];
    	[passwd release];
    	[usrString release];
    	[passwdString release];
        [super dealloc];
    }
    
    
    @end
     
  24. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #24
    Friendly Reminder:

    When posting code to the forum it is extremely helpful for those who are trying to help you if you enclose your code in the [code][/code] tags.


    There are two ways to do this

    1. Write out the [code]Your code here[/code] tags manually
    2. Use the code button [​IMG] and paste your text into the dialog

    Please be courteous to those attempting to help you and heed this advice.
     
  25. eatpie26 thread starter macrumors newbie

    Joined:
    Jan 4, 2011
    #25
    The problem is that the array seems to be resetting itself every time I press the button. How do I fix this?
     

Share This Page