Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Ericson Ortega
Ericson Ortega
3,307 Points

how to show 6 items from array?

How can I show 6 items from an array?

Hey Ericson! I'm not sure I understand your question. Are you trying to create an array with 6 items? Are you using Objective-C? If this corresponds to a code challenge, can you provide the link?

Ericson Ortega
Ericson Ortega
3,307 Points

What I wanted to do is to pick 6 items from the list of available items. For example I have number from 1-50 and I wanted to pick 6 number from the list how can I do that?

This is not from the code challenge I am trying to experiment and edit the crystal ball sample app.

4 Answers

You can either create 5 more labels like the one you currently have and set the text of each of those labels individually, or you can set the text of your current label to contain six predictions using NSString's stringWithFormat method. See below: (this is in the makePrediction method in THViewController.m)

self.predictionLabel.text = [NSString stringWithFormat:@"%@\n%@\n%@\n%@\n%@\n%@",
            [self.crystalBall randomPrediction], [self.crystalBall randomPrediction], [self.crystalBall randomPrediction],
            [self.crystalBall randomPrediction], [self.crystalBall randomPrediction], [self.crystalBall randomPrediction]];

You may have to adjust your label to view all of the text.
(Side note: There may be a better way to do this, but this gets the job done.)

Ericson Ortega
Ericson Ortega
3,307 Points

Why didn't I think of that. Thank You.

Are you looking to get specific items or six random items?

If you want to retrieve a specific item you can use the objectAtIndex method on the array.

For example:

NSArray *names;
NSString *string;

names = [[NSArray alloc] initWithObjects: @"Bob", @"Tom", @"Mary", nil];
string = [names objectAtIndex: 1];   // This contains the value @"Tom"
Ericson Ortega
Ericson Ortega
3,307 Points

Random. Also instead of displaying 1 item I wanted to display 6 items.

What do you want to do with the 6 items? Are you storing it to six separate variables? Or do you want to create a new array that has just those six items?

Ericson Ortega
Ericson Ortega
3,307 Points

This i my code:

```- (NSArray *) predictions { if (_predictions == nil) { _predictions = [[NSArray alloc]initWithObjects:@"It is Certain", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11", @"12", nil]; } return _predictions; }

But instead of showing only one random item I wanted to view 6 random items.