NSMutableArray only adds one object!

Discussion in 'iOS Programming' started by Oneill, Oct 2, 2013.

  1. Oneill macrumors newbie

    Joined:
    Nov 10, 2012
    #1
    - I want to add objects to the property list I created which in my case called (highscoreArray) but what the problem I'm facing is that it adds the object then release it, in other words the next time I check the 'count' input it keeps showing only the number 1(see the Check method below) .
    Code:
      
    // Lazy Instantiation.
    -(NSMutableArray *)highscoreArray
    {
        if(!_highscoreArray) _highscoreArray=[[NSMutableArray alloc]init];
        return _highscoreArray;
    }
    
    -(void)Arraywithscores:(int)score
    {  NSNumber *point = [NSNumber numberWithInt:score];
        [self.highscoreArray addObject:point];
        [self Check:self.highscoreArray];
    }
    
    -(void)Check:(NSMutableArray *)highscoreArray
    {
        NSLog(@"The number is %i",[highscoreArray count]);
    }
    - Any solutions please ?
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    You're not sharing enough with us... I don't see how your Check: or Arraywithscores: methods could be called multiple times.

    On a side note, these method names are horrid:
    1 - Don't capitalize the first letter of method names.
    2 - Do capitalize other letters to signal where new words start.
    3 - Don't pluralize the last word of a method name if it only takes a single, non-collection argument.

    Your methods should be called check: and arrayWithScore:
     
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    Coding Guidelines for Cocoa.

    It's mainly about naming.
     
  4. Oneill thread starter macrumors newbie

    Joined:
    Nov 10, 2012
    #4
    Thank you for the note appreciate it though this is a quick unorganized code.
    More details about the project:
    -It is a quiz game a right question increase the score by 10, so basically what I have is a score label' obviously for displaying the score via the score property' and a timer once time is up the score must be added to the array.
    thats all !
    Input:
    2013-10-02 21:05:07.246 Project[989:c09] The number is 1
    The old scores are all gone though as you can see.
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    Post your @interface for the class, showing the instance variable and the property declarations.

    Also post the init method for the class, and any/all synthesized property accessors.

    ----------

    Organize it, then post it.

    What you've posted is insufficient to diagnose anything. The first rule of getting advice about code is "Post your code". Not just the code you think is relevant, but all of it.

    Here's why you post all your code. If you don't understand everything that's happening, then you can't accurately decide what's relevant code and what isn't. A frequent result is that you leave out something that's actually important, but because no one sees it, and you didn't say that code exists, the error-causing code never gets seen.

    Read this and follow its guidelines:
    http://www.mikeash.com/getting_answers.html
     
  6. devilofspades macrumors member

    Joined:
    Jul 20, 2011
    #6
    don't know if you figured this out yet, but just make sure you have a property for your array

    Code:
     @property (strong, nonatomic) NSMutableArray *highscoreArray; 
    the formatting of your code could use some work, but the functionality is there. just make sure you have a reference to the array. anything else inside the array will have a strong pointer to it by default.
     

Share This Page