Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

objective c - Cannot add object to an NSMutableArray array

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...