简体   繁体   中英

iOS loop through NSMutableArray of objects

Is there a efficient way too loop through an NSMutableArray containing custom objects?

This is how to init and add objects to my NSMutableArray :

self.messages = [[NSMutableArray alloc] initWithObjects:nil];
[self.messages addObject:[[JSMessage alloc] initWithText:message sender:name date:myDate livePreview:@"1"]];

What i want to do specifically is to loop through the NSMutableArray and look for where the "livePreview" key is 1 and then update the message on that object.

How could this be done?

for (JSMessage * message in self.messages) {
   if ([message.livePreview isEqualToString:@"1"]) {
      message.message = @"updated message";
   }
 }

您还可以看一下NSArray基于块的API来检查元素

- (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block

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