Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Objective-C Basics Basic Data Types and Variables Numerical Data Types: ints, floats and doubles

What does all of the code at the top mean?

I tried using things like NSString without all that code, except for the #import foundation part, and nothing works, and following exactly the same to the video i get errors like --

'type specifier missing, defaults to int', and 'unknown type name NSString'.

So obviously this code here at the top is important, but why? Can someone run me through it?

import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... NSLog(@"Hello, World!"); } return 0; }

2 Answers

#import <Foundation/Foundation.h>

Tells the compiler to first load the code found in Foundation.h. This is code that has been written by Apple and includes additional variable types and methods to use on those such as what an NSString is and how to use it.

int main(int argc, const char * argv[])

This is the start place for the code you write to run. The int at the start says that the program will return an int value when it finishes running its code. More about that at the end. main is the name of this program and is how the compiler knows where to start running code. int argc and const char * argue[] both refer to arguments or variables that you would pass to your program if you were to run it at the command line. An integer value and characters.

@autoreleasepool

Tells the compiler to create an autorelease pool which is a place where it stores variables that it will later give up or release the memory it is using for at a later time automatically.

// insert code here...

This is a comment that is for you and future programmers to read is ignored by the compiler.

NSLog(@"Hello, World!");

This logs or prints the message Hello, World! to the console.

return 0;

This tells the program that it has ended and the reason it returns a 0 is to say that it exited without error.

The various { } throughout show where a block of code begins and ends. They are always in pairs, opening and closing.

Everything after int main(int argc, const char * argv[]) and before return 0; can be safely removed and is part of the program template provided by Xcode and is not required for your own programs.