Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Creating a Data Collection - Implementing an Array incompatible pointer types assigning to NSString from NSArray

Hey Guys, I am having an issue assigning my NSArray to my prediction Label. Xcode is saying "incompatible pointer types assigning to NSString from NSArray". Can somebody please take a look at my code an tell me what I am doing wrong.

  • (IBAction)buttonPressed { NSArray *predictions = [[NSArray alloc] initWithObjects:@"It is Certain",@"Decidedly so", @"The stars are not aligned",@"My reply is no",@"It is doublful",@"Better not tell you now",@"Concertrate and ask again",@"Unable to answer now", nil ];

    self.predictionLabel.text = [predictions objectsAtIndexes:0];

}

1 Answer

Because objectsAtIndexes returns an array:

- (NSArray *)objectsAtIndexes:(NSIndexSet *)indexes

Are you trying to just get the value at position 0? Then try objectAtIndex;

   [predictions objectAtIndex:0];

Also sometimes you may need to cast it, so that it knows that the object at that position is of a certain type.

self.predictionLabel.text =(NSString*)[predictions objectAtIndex:0];

Thanks So Much Kerde. I can see the mistake I made now.

No problem!