简体   繁体   English

iOS通过对象的NSMutableArray循环

[英]iOS loop through NSMutableArray of objects

Is there a efficient way too loop through an NSMutableArray containing custom objects? 有没有一种有效的方法也可以遍历包含自定义对象的NSMutableArray

This is how to init and add objects to my NSMutableArray : 这是初始化对象并将其添加到我的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. 我要专门执行的操作是遍历NSMutableArray并查找“ livePreview”键为1的位置,然后更新该对象上的消息。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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