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

Objective-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
STAFF
Amit Bijlani
Treehouse Guest Teacher

Sorry 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]);

Actually nvm, this ended up working for me

NSNumber *foxtrot = @24; NSLog (@"foxtrot %@", foxtrot);

Change %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."

Thanks. NSLog (@"foxtrot %@", foxtrot); worked perfectly!

Thank you!

Sorry, 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]);

NSNumber * foxtrot; foxtrot = [[NSNumber alloc] init]; foxtrot = @24; NSLog (@"foxtrot %@",foxtrot);

It's worked!