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 trialStuart Robertson
Courses Plus Student 1,296 PointsWhat 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
Mark Reid
1,948 Points#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.
Akash Sharma
Full Stack JavaScript Techdegree Student 14,147 Pointsa functional program?