Mac NSMutableArray count returns 0

TheHeavySoldier

macrumors newbie
Original poster
Mar 8, 2014
10
0
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. :)
 

xStep

macrumors 68020
Jan 28, 2003
2,005
99
Less lost in L.A.
I'd say the NSMutableArray is already created ("declared"); it just needs to be alloc'ed and init'd still.
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. :)
 

TheHeavySoldier

macrumors newbie
Original poster
Mar 8, 2014
10
0
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.
 

chown33

Moderator
Staff member
Aug 9, 2009
8,497
4,458
Pale blue comma
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.
You should also read this about naming:
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingBasics.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.