How to split an NSArray into two equal pieces?

前端 未结 3 614
忘了有多久
忘了有多久 2020-12-06 10:33

I have an NSArray, and I want to split it into two equal pieces (if odd \"count\" then add to the latter new array) - I want to split it \"down the middle\" so to speak.

3条回答
  •  渐次进展
    2020-12-06 11:11

    Have you tried adding nil to the end of the -initWithObjects: method?

    NSArray *leftArray = [[NSArray alloc] initWithObjects:[testableArray objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(????, ????)]], nil];
    NSArray *rightArray = [[NSArray alloc] initWithObjects:[testableArray objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(????, ????)]], nil];
    

提交回复
热议问题