-2

Hi i want to implement my own Objects to manage my data, i was trying to make a two classes.

Class Continents that contains a Continent Objects

Here is my implementation:

@implementation OsContinents
@synthesize continentes;


-(id)init{
    return [super init];
}

-(NSUInteger)count{
    NSLog(@"%u",[continentes count]);
    return [continentes count];
}
-(void)add:(OsContinent *)continente{
    [continentes addObject:continente];
}

-(OsContinent *)getElementByIndex:(NSUInteger)index{
    return [continentes objectAtIndex:index];
}

-(void)deleteContinentByIndex:(NSUInteger)index{
    return [continentes removeObjectAtIndex:index];
}

-(void)deleteContinent:(OsContinent *)objContinent{
    return [continentes removeObject:objContinent];
}

-(NSMutableArray *)getAll{
    return continentes;
}

@end

Next i want to populate *continents Property with "Continent" Objects like this.

OsContinents *continentesCollection = [[OsContinents alloc] init];
    for (NSString *strContinente in [data allKeys]) {
        OsContinent *con = [[OsContinent alloc] init];
        [con setContinente:strContinente];
            NSLog(@"%@",[con getContinente]);
        [continentesCollection add:con];
    }
    NSLog(@"%u",[continentesCollection count]);

But allways got ZERO in de count Method.

Note: NSLog(@"%@",[con getContinente]) print de data OK, the Continent Object is OK, the problem is in the "*continentes" inside the Continents Object-

Any Clue?

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
OsDev
  • 1,243
  • 9
  • 20
  • 1
    why don't you just use `NSMutableArray`? – danqing Aug 18 '12 at 19:05
  • @iBlue Because i wan't to implement a dataSource and i want to extend the functionality of my Data Collection – OsDev Aug 18 '12 at 19:08
  • and I don't see a `getContinent` method anywhere. – danqing Aug 18 '12 at 19:13
  • @iBlue getContinent it's a magic getter that synthethize make for me, and get the *continent Property of Continent Obj – OsDev Aug 18 '12 at 19:16
  • possible duplicate of [NSMutableArray addObject not affecting count?](http://stackoverflow.com/questions/3683761/nsmutablearray-addobject-not-affecting-count), [Cannot add items to an NSMutableArray ivar](http://stackoverflow.com/questions/7125326/cannot-add-items-to-an-nsmutablearray-ivar), [NSMutableArray addObject: not working](http://stackoverflow.com/questions/1827058/nsmutablearray-addobject-not-working) – jscs Aug 18 '12 at 19:16
  • Don't use a category! Then you'd be polluting NSMutableArray throughout your app. Containment is the right approach. – Jon Reid Aug 18 '12 at 19:16
  • @W'rkncacnter Maybe in the init Method of Continents.m i can initialize NSMutableArray ? like this "continentes = [[NSMutableArray alloc] init]; – OsDev Aug 18 '12 at 19:24
  • @W'rkncacnter it's woooooootks i HAVE TO initialize de NSMutableArray, woooow, thank you – OsDev Aug 18 '12 at 19:25

1 Answers1

1

Your initializer does nothing but initialize the superclass. Use it to set up your own class:

- (id)init
{
    self = [super init];
    if (self)
    {
        _continentes = [[NSMutableArray alloc] init];
    }
    return self;
}

Otherwise, continentes will remain nil. Messaging nil is valid: methods simply don't do anything, and return 0.

If you want to completely hide the underlying mutable array (which is perfectly fine) then don't advertise it in your .h file as a property. Instead, at the beginning of your @implementation, declare a semi-private instance variable:

@implementation OsContinents
{
    NSMutableArray *_continentes;
}

I say "semi-private" because you can always use the runtime engine to introspect objects. But it'll be hidden from normal use. If you ever subclass your object, you can always move the instance variable declaration from your @implementation to your @interface so that subclasses can get at it.

Jon Reid
  • 20,545
  • 2
  • 64
  • 95