Pregunta de entrevista de GREE International Entertainment

Here's some broken code, describe what's wrong with it: NSMutableArray * objects; // .... fills out objects with ReadyForDeletionObject objects for (uint i=0; i<[objects count]; i++) { ReadyForDeletionObject * object = [objects objectAtIndex: i]; if (object.shouldBeRemoved) { [objects removeObjectAtIndex: i]; } }

Respuesta de la entrevista

Anónimo

31 oct 2012

// objects needs to be created NSMutableArray * objects = [[NSMutableArray alloc] init]; // "uint" isn't the best type to use // // but more importantly, if we were doing regular enumeration // (that is, "forward enumeration"), we'd likely skip over // the next array entry after removing the previous one // // it's smarter to do a reverse enumeration for (NSUInteger i=[objects count]; i>0; --i) { ReadyForDeletionObject * object = [objects objectAtIndex: i]; if (object.shouldBeRemoved) { [objects removeObjectAtIndex: i]; } }