NSMutableArray count returns 0

Discussion in 'Mac Programming' started by TheHeavySoldier, Mar 12, 2014.

  1. TheHeavySoldier macrumors newbie

    Joined:
    Mar 8, 2014
    #1
    Hey everyone,

    I'm just messing around with the NSMutableArray function, and I'm trying to return the array count, but it keeps returning 0.
    I've added in printf functions to make sure the addObject methods are actually ran, and they do (well the loop is run correctly, and I don't receive any warnings or errors.)

    My code:
    initialSetup.h
    Code:
    #import <Foundation/Foundation.h>
    
    @interface initialSetup : NSObject
    
    @property NSMutableArray *xMapSize, *yMapSize;
    
    -(void)setupMapXSize: (int)x YSize: (int)y;
    -(void)printMap;
    -(void)returnMapSize;
    
    @end
    
    initialSetup.m
    Code:
    #import "initialSetup.h"
    
    @implementation initialSetup
    
    @synthesize xMapSize, yMapSize;
    
    -(void)setupMapXSize: (int)x YSize: (int)y {
        for (int I = 1; I <= x; I++)
            [xMapSize addObject: [NSNumber numberWithInt: I]];
        for (int O = 1; O <= y; O++)
            [yMapSize addObject: [NSNumber numberWithInt: O]];
    }
    
    -(void)printMap {
        // Hmmmmm..
    }
    
    -(void)returnMapSize {
        unsigned long xSize = [xMapSize count],
                      ySize = [yMapSize count];
        printf("(%lu, %lu)\n", xSize, ySize);
    }
    
    @end
    
    main.m
    Code:
    #import <Foundation/Foundation.h>
    #import "initialSetup.h"
    
    int main(int argc, const char * argv[])
    {
        @autoreleasepool {
            initialSetup *graphMap = [[initialSetup alloc] init];
            
            [graphMap setupMapXSize: 10 YSize: 10];
            [graphMap returnMapSize];
        }
        return 0;
    }
    
    The console returns:
    Code:
    (0, 0)
    Program ended with exit code: 0
    
    I have a feeling there is something wrong with the variables in my returnMapSize method.
    If someone could give me a hint, I'd appreciate it. :)
     
  2. waterskier2007 macrumors 68000

    waterskier2007

    Joined:
    Jun 19, 2007
    Location:
    White Lake, MI
    #2
    You need to manually create the NSMutableArrays

    See this
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    I'd say the NSMutableArray is already created ("declared"); it just needs to be alloc'ed and init'd still.
     
  4. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #4
    The NSMutableArray properties exist, yes, but since no alloc has occurred, they point to nothing so are nil and therefore not created. I argue that the creation being talked about happens upon the alloc. :)
     
  5. waterskier2007 macrumors 68000

    waterskier2007

    Joined:
    Jun 19, 2007
    Location:
    White Lake, MI
    #5
    Good call. Bad choice of terminology on my part, but the idea was the same
     
  6. TheHeavySoldier thread starter macrumors newbie

    Joined:
    Mar 8, 2014
    #6
    Hey guys, thank you for your replies.

    I've created a initialize method to alloc and init the xMapSize and yMapSize, and it returns the correct value.

    Code:
    #import "initialSetup.h"
    
    @implementation initialSetup
    
    @synthesize xMapSize, yMapSize;
    
    -(void)initGraph {
        xMapSize = [[NSMutableArray alloc] init];
        yMapSize = [[NSMutableArray alloc] init];
    }
    
    Thanks guys.
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    You should also read this about naming:
    https://developer.apple.com/library...cs.html#//apple_ref/doc/uid/20001281-BBCHBFAH

    Most obviously, your class name doesn't contain a noun (it's an adjective-verb), and doesn't start with upper-case.

    If the class is a graph, then that's a noun, so use that in its name (with suitable capitalization).

    Your returnMapSize method also doesn't return anything. It prints something, but returning something has a specific meaning in C and Objective-C, which you may not yet know about. Method or function names should not be overtly deceptive.
     

Share This Page