how to create NSMutableArray dynamically based on certain condition in iphone
I have a array of dates, so based on certain condition of date I need to create a NSmutablearray and add some values to that particular array. so number of sections in my tableview depends on this mutablearray count(which is internally depends on condition of date).
开发者_如何学Pythonfor example:
if(date1)
1.create array1
2.add x,y,z values to the array1.
if(date2)
1.create array2
2.add xx,yy,zz values to the array2.
....
thanks..
NSMutableArray *a = [NSMutableArray array];
if (b) {
[a addObject: ... some object based on "b" being true ...];
... etc ...
} else if (c) {
[a addObject: ... some object based on "c" being true ...];
... etc ...
} else {
[a addObject: ... some object based on neither "c" nor "b" being true ...];
... etc ...
}
[Responding to LordT's comment]
"Don't" is a strong word considering that
NSMutableArray *aMutableArray = [NSMutableArray array]; // compiles but invokes +array in NSMutableArray's parent class since +array is not declared in NSMutableArray. What you get if you inspect the object returned by the message expression is an NSArray.
If you really want to use +array but not NSMutableArray class method +arrayWithCapacity: , perhaps
NSMutableArray *aMutableArray = [[NSMutableArray array] mutableCopy];
will suffice? Just don't forget to memory manage. 8)
精彩评论