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 trialOcky Wiemeijer
5,451 Pointsretrieving string from Array and assigning it to an NSString
Hi Guys,
What am I doing wrong here (at line 10)? I need to get the string "eggs" from the NSArray shoppingList and put it in the NSString shoppingCart.
Note: adding self(dot) to shoppingList in line 10 does not work
#import "UIViewController.h"
@interface ViewController : UIViewController
@property (strong, nonatomic) NSString *shoppingCart;
@property (strong, nonatomic) NSArray *shoppingList;
@end
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Add your code below!
self.shoppingList = [[NSArray alloc] initWithObjects:@"toothpaste", @"bread", @"eggs", nil];
self.shoppingCart.text = [shoppingList objectAtIndex:2];
}
@end
1 Answer
Steve Hunter
57,712 PointsHi there,
You're very close - but there's no need to add .text
to the variable:
self.shoppingList = [[NSArray alloc] initWithObjects:@"toothpaste", @"bread", @"eggs", nil];
self.shoppingCart = [self.shoppingList objectAtIndex: 2];
Hope that helps,
Steve.
Madhukar Narahari
8,965 PointsMadhukar Narahari
8,965 PointsHi Steve,
Could you let me know why we took that ".text" away?
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsHi Madhukar,
There.s no need for a
.text
. The variable,shoppingCart
is declared as a String. A String object doesn't use a.text
method to assign a string value to it. The array methodobjectAtIndex:
returns the contents of the array at the specified element position. In this case, that returns a String which is assigned directly into the string variable that had already been declared.I hope that makes sense!
Steve.