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 Object-Oriented Objective-C Memory, Arrays and Loops, Oh My! Review Dictionaries and Arrays

sonu dhawan
sonu dhawan
1,989 Points

How to add an array of items into an NSMutableDictionary .

variable_assignment.mm
NSArray *shoeOrder = @[@"Charles Smith", @(9.5), @"loafer", @"brown",nil];
NSMutableDictionary *shoeOrderDict=[ [NSMutableDictionary alloc] init ] ;

for(NSNumber i = [shoeOrder] ; i <= [shoeOrder count];i++)
    {
      shoeOrderDict[@"@%",i] = [shoeOrder objectAtIndex:"%i",i];
    }

1 Answer

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

To be honest, I did not have the time to watch the videos to this challenge, so there might be other aspects to this assignment. The straight forward way would be:

NSArray *shoeOrder = @[@"Charles Smith", @(9.5), @"loafer", @"brown"];
NSMutableDictionary *shoeOrderDict = [[NSMutableDictionary alloc] init];
[shoeOrderDict setObject:[shoeOrder objectAtIndex:0] forKey:@"customer"];
[shoeOrderDict setObject:[shoeOrder objectAtIndex:1] forKey:@"size"];
[shoeOrderDict setObject:[shoeOrder objectAtIndex:2] forKey:@"style"];
[shoeOrderDict setObject:[shoeOrder objectAtIndex:3] forKey:@"color"];

Let me walk you through your code, nevertheless:

// A: don't use NSNumber, but NSInteger, which is a primitive and
//     is expected by `objectAtIndex` as well
// B: Tbh, I have no clue what you expected [shoeOrder] to return,
//     but it's definitely a syntax error. My guess is you want to iterate
//     over the array, but you can hardcode the 0 there, because that
//     is always the first index of an array

for(NSNumber i = [shoeOrder] ; i <= [shoeOrder count]; i++) {  

    // [@"@%",i] is not valid syntax either, I think you wanted to do this:
    // [NSString stringWithFormat:@"%i", i] to create a string from an integer
    // objectAtIndex expects an NSInteger, "%i",i below is a syntax error   
    shoeOrderDict[@"@%",i] = [shoeOrder objectAtIndex:"%i",i];
}

That was much information, I hope it helps :)

Martin Wildfeuer
Martin Wildfeuer
Courses Plus Student 11,071 Points

Oh boy, the editor totally messes up the code, so please imagine the for loop was there and written like you did it ;)

sonu dhawan
sonu dhawan
1,989 Points

Much obliged for the assistance martin.

sonu dhawan
sonu dhawan
1,989 Points

what about this code :

NSArray *shoeOrder = @[@"Charles Smith", @(9.5), @"loafer", @"brown",nil];
NSMutableDictionary *shoeOrderDict=[ [NSMutableDictionary alloc] init ] ;

for(NSInteger i = [shoeOrder objectAtIndex:0] ; i <= [shoeOrder count];i++)
    {
      shoeOrderDict[NSString stringWithFormat:@"%i", i] = [shoeOrder objectAtIndex:"%i",i];
    }
Martin Wildfeuer
Martin Wildfeuer
Courses Plus Student 11,071 Points
// What you are trying to do here...
NSInteger i = [shoeOrder objectAtIndex:0];

// is basically this
NSInteger i = @"Charles Smith"

I recommend creating a test project in Xcode, the compiler will warn you about errors right away.