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
Thomas Nilsen
14,957 PointsObjective-C Basics stage 5, task 2/4
Hi!
This is just driving me crazy. Here is the challenge:
Print the result using NSLog where the output should look like the following: "foxtrot 24". (Remember to use the '%d' format specifier when printing the value of 'foxtrot'.
Here is my code:
NSNumber *foxtrot;
foxtrot = [[NSNumber alloc] initWithInt:24];
NSLog(@"foxtrot %d", foxtrot);
Where am I going wrong?
6 Answers
Amit Bijlani
Treehouse Guest TeacherSorry you are getting frustrated. The video ends in showing you how to reference the documentation, so for this challenge you are supposed to refer to the documentation.
When using the format specifier '%d' it is expecting an int value. When you are printing out an object, you simply use '%@' which sends a message to the description method of the object. To print out the int value of a NSNumber object you have to convert the object into an int. Just like you converted the int into a number object when you allocated and initialized it.
NSLog(@"foxtrot %d",[foxtrot intValue]);
Colton Whited
516 PointsActually nvm, this ended up working for me
NSNumber *foxtrot = @24; NSLog (@"foxtrot %@", foxtrot);
John Hill
Front End Web Development Techdegree Graduate 35,236 PointsChange %d to a %@ you must.
More NSNumber info here: http://rypress.com/tutorials/objective-c/data-types/nsnumber.html
Maybe a good idea: "As you could probably imagine, this isn’t the most efficient way to work with numbers. In real applications, you should limit yourself to primitive numeric types for computationally intensive algorithms and wait as long as possible to store the result in an NSNumber container."
Rohan A.
4,008 PointsThanks. NSLog (@"foxtrot %@", foxtrot); worked perfectly!
Thomas Nilsen
14,957 PointsThank you!
Colton Whited
516 PointsSorry, but this stil didn't work for me. It says I have the correct string, but the incorrect format. Any ideas?
NSNumber *foxtrot = @24; NSLog (@"foxtrot %d", [foxtrot intValue]);
Daisy Nguyen
Courses Plus Student 3,525 PointsNSNumber * foxtrot; foxtrot = [[NSNumber alloc] init]; foxtrot = @24; NSLog (@"foxtrot %@",foxtrot);
It's worked!