How to print NSArray objects via statement or enumeration?

Possible duplicate:
How to iterate over NSArray?

Here is my code (for example):

NSArray *myArray = [NSArray arrayWithObjects:@"Red", @"Blue", @"Green", nil]; 

I want the loop through the array to print each line in the console.

Thanks.

+6
source share
2 answers

Find the most difficult way, will we?

 NSArray *myArray = [NSArray array]; id *objects = malloc(sizeof(id) * myArray.count); [myArray getObjects:objects range:NSMakeRange(0, myArray.count)]; char **strings = malloc(sizeof(char *) * myArray.count); for (int i = 0; i < myArray.count; i++) { strings[i] = [objects[i] UTF8String]; } printf("<"); for (int i = 0; i < myArray.count; i++) { printf("%s" strings[i]); if (i != myArray.count - 1) printf(", "); } printf(">"); free(objects); free(strings); 

Of course, you can always just do it like this:

 NSLog(@"%@", myArray); 
+18
source

The easiest way:

 NSLog(@"%@", myArray); 

Or, if you want to use quick enumeration to print each object in its own way:

 for (NSString *string in myArray) { NSLog(@"%@", string); } 
+25
source

Source: https://habr.com/ru/post/903202/


All Articles