简体   繁体   中英

Problem removing multiple objects from NSMutableArray iPhone SDK

I am having trouble removing objects from nsmutable array. Here it is.

NSURL *url = [NSURL URLWithString:@"http://www.lockerz.com/dailies"]];
NSData *datadata = [NSData dataWithContentsOfURL:url];  
NSString *removeForArray = [[NSString alloc] initWithData:datadata encoding:NSASCIIStringEncoding];
NSArray *theArray = [removeForArray componentsSeparatedByString:@" "];  
NSMutableArray *deArray = [[NSMutableArray array] initWithArray:theArray];
[deArray removeObjectsInRange:NSMakeRange(0, 40)];  
NSLog(@"%@", deArray);

+[NSMutableArray array] already returns an initialized array. Don't use an initializer method on that, they are used on new instances that you alloc d.

In this case you can either

  • alloc / init an instance
  • use -mutableCopy
  • use a suitable convenience constructor

The three following lines are equivalent:

NSMutableArray *a = [[theArray mutableCopy] autorelease];
NSMutableArray *b = [NSMutableArray arrayWithArray:theArray];
NSMutableArray *c = [[[NSMutableArray alloc] initWithArray:theArray] autorelease];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM