Cannot add object to an NSMutableArray array

痞子三分冷 提交于 2019-11-30 09:23:38

问题


@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?


回答1:


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;
}



回答2:


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



来源:https://stackoverflow.com/questions/9658679/cannot-add-object-to-an-nsmutablearray-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!