2

Say I had an array of strings...

[NSArray arrayWithObjects: @"red", @"blue", @"green", @"yellow", nil]

how would I achieve an output like this...?

red is a color
blue is a color
green is a color
yellow is a color

Thanks!

2
  • What language do you want it in? Commented Apr 8, 2011 at 2:53
  • Where do you want this output to go? To a file, to stdout, to stderr? Commented Apr 8, 2011 at 5:48

2 Answers 2

7
NSArray *colors = ...;
for (NSString *color in colors) {
  NSLog(@"%@ is a color", color);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Don't use NSLog() for ordinary string output. It prepends a lot of information that the question doesn't ask for.
Well i'm using Obj c in xcode, I need the output to be in NSString format so that i can display the list of results in a textView
0
NSArray *initializedNSArray = [NSArray arrayWithObjects: @"red", 
                                                         @"blue", 
                                                         @"green", 
                                                         @"yellow", 
                                                         nil]
for( int i=0; i<4; ++i )
    NSLog(@"%@ is a color \n", [initializedNSArray objectAtIndex: i];
                             // ^^^^^^^^^^^^^^^^^  to array it is earlier initialized to

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.