4
@interface SimataDetailViewController () 

@property Simata *simata;

@property (nonatomic, copy) NSMutableArray *simataList;

@end

@implementation SimataDetailViewController

@synthesize simataDataController=_simataDataController;
@synthesize category=_category;
@synthesize simata=_simata;
@synthesize simataList=_simataList;

#pragma mark - Managing the detail item



- (void) getSimataForCategory: (NSString *) inputCategory {

    unsigned count = [self.simataDataController.masterList2 count];


    while (count--) {

        if ([[[self.simataDataController objectSimataInListAtIndex:count] categoryCode] isEqual:inputCategory]){
            self.simata= [self.simataDataController objectSimataInListAtIndex:count];

            [self.simataList addObject:self.simata];                       
        }

    }

    NSLog(@"count, %u", [self.simataList count]);


}

Hello this is my first post, so please be patient.

I am trying to add object self.simata to array self.simataList but the array stays with zero objects. The object self.simata is not nil and I don't get any error.

What am I doing wrong?

Kostas
  • 1,504
  • 1
  • 11
  • 13
  • (+1 credit for making a fairly intelligible post as a newbie.) – Hot Licks Mar 11 '12 at 20:43
  • possible duplicate of [NSMutableArray addObject not working](http://stackoverflow.com/questions/1827058/nsmutablearray-addobject-not-working) and [Cannot add items to an NSMutableArray](http://stackoverflow.com/questions/7125326/cannot-add-items-to-an-nsmutablearray-ivar) – jscs Mar 12 '12 at 03:18

2 Answers2

5

Are you sure you have created an instance of the array before you use it?

self.simataList =[NSMutableArray array];

It could also be your self.simata being nil...

EDIT

You could create the array instance in the default init method:

-(id)init
{
    self = [super init];
    if(self)
    {
        //do your object initialization here
        self.simataList =[NSMutableArray array];
    }
    return self;
}
EsbenB
  • 3,356
  • 25
  • 44
  • And where should I create the instance; I want array's scope to be all over the class. – Kostas Mar 11 '12 at 20:47
  • creating an init method and placing it there is most likely the best solution in your case. See above... – EsbenB Mar 11 '12 at 20:50
  • It worked. I had to create an instance using the statement : *self.simataList =[NSMutableArray array];*. Thanks for your help! – Kostas Mar 11 '12 at 20:51
  • glad it worked. You can use break points in Xcode and hover over variables to check if they have been created. if you see a nil pointer (0x0) it generally means that you haven't initiated the object correctly. – EsbenB Mar 11 '12 at 20:57
1

Most likely self.simataList is nil. Try NSLogging self.simataList itself, rather than its count.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151