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 Dynamic Typing Code Challenge

Use NSLog to print out the value of the variable 'thing'. The output should look like: "thing = 4".

And what have you tried so far?

4 Answers

NSLog, as you know, outputs the string to the console/log. To output the string Hello World, you do NSLog(@"Hello World");

You can dynamically replace part of it using format strings, which are preceded with the percent symbol. To output the string Our number is #, where # should be replaced by some number from a variable variable, you do

NSLog(@"Our number is %d", variable);

If variable has the value 5, %d would be replaced by the number 5, the end result is "Our number is 5"

(%d is used for integer, other options include %f for float, %@ for Objective-C object, %c for a single character, etc)

3 years of development with Java and JavaScript makes me forget the "@" in front of string. Thanks clared my quiz :)

I am having the same problem! I am putting the code like this:

id thing = nil;

thing = [[NSNumber alloc] initWithInt:4];

NSLog = (@"thing = %@", thing);

and the error is:

Bummer! Compiler error! Make sure you are passing an 'NSString' to NSLog with the correct format, and check your syntax!

Somebody help!!!

Take out the = after NSLog

can't believe I did that hahahaha thanks!

so far i did the Dynamic Typing Code Challenge. I completed task 1 and task 2 but having trouble with task 3 where i have Use NSLog to print out the value of the variable 'thing'. The output should look like: "thing = 4".

need help please------>@

when i print out NSLog(@" thing is %d", thing) it says "not the correct input to NSLog"

Close! The question asks for an output of the format thing = #, what you had there would print thing is #