Ricerca…


Enumerazione rapida di un NSArray

Questo esempio mostra come usare l'enumerazione veloce per attraversare un NSArray.

Quando hai un array, ad esempio

NSArray *collection = @[@"fast", @"enumeration", @"in objc"];

Puoi usare il for ... in sintassi per passare attraverso ogni elemento dell'array, iniziando automaticamente con il primo all'indice 0 e fermandosi con l'ultimo elemento:

for (NSString *item in collection) {
    NSLog(@"item: %@", item);
}

In questo esempio, l'output generato sarebbe simile

// item: fast
// item: enumeration
// item: in objc

Enumerazione rapida di un NSArray con indice.

Questo esempio mostra come usare l'enumerazione veloce per attraversare un NSArray. In questo modo puoi anche tenere traccia dell'indice dell'oggetto corrente mentre attraversi.

Supponiamo che tu abbia un array,

NSArray *weekDays = @[@"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday", @"Sunday"];

Ora puoi attraversare l'array come di seguito,

[weekDays 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

}];


Modified text is an extract of the original Stack Overflow Documentation
Autorizzato sotto CC BY-SA 3.0
Non affiliato con Stack Overflow