Ausgabe
Ich möchte den Index des aktuellen Objekts erhalten, wenn ich die schnelle Aufzählung verwende, dh
for (MyClass *entry in savedArray) {
// What is the index of |entry| in |savedArray|?
}
Lösung
Sehen Sie sich die API für NSArray an und Sie werden die Methode sehen
- (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block
Probieren Sie es also aus
[savedArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
//... Do your usual stuff here
obj // This is the current object
idx // This is the index of the current object
stop // Set this to true if you want to stop
}];
Beantwortet von – Paul.s
Antwort geprüft von – David Marino (FixError Volunteer)