This Objective-C® Notes for Professionals book is compiled from Stack Overflow Documentation, the content is written by the beautiful people at Stack Overflow. Text content is released under Creative Commons BY-SA, see credits at the end of this book whom contributed to the various chapters. Images may be copyright of their respective owners unless otherwise specified
This is an unofficial free book created for educational purposes and is not affiliated with official Objective-C® group(s) or company(s) nor Stack Overflow. All trademarks and registered trademarks are the property of their respective company owners
The information presented in this book is not guaranteed to be correct nor accurate, use at your own risk
Please send feedback and corrections to [email protected]
—
This program will output “Hello World!”
#import
int main(int argc, char * argv[]) {
NSLog(@”Hello World!”);
}
#import is a pre-processor directive, which indicates we want to import or include the information from that file into the program. In this case, the compiler will copy the contents of Foundation.h in the Foundation framework to the top of the file. The main difference between #import and #include is that #import is “smart” enough to not reprocess files that have already been included in other #includes.
The C Language documentation explains the main function.
The NSLog() function will print the string provided to the console, along with some debugging information. In this case, we use an Objective-C string literal: @”Hello World!”. In C, you would write this as “Hello World!”, however, Apple’s Foundation Framework adds the NSString class which provides a lot of useful functionality, and is used by NSLog. The simplest way to create an instance of NSString is like this: @”>string content here”.
Technically, NSLog() is part of Apple’s Foundation Framework and is not actually part of the Objective-C language. However, the Foundation Framework is ubiquitous throughout Objective-C programming. Since the Foundation Framework is not open-source and cannot be used outside of Apple development, there are open-source alternatives to the framework which are associated with OPENStep and GNUStep.
Compiling the program
Assuming we want to compile our Hello World program, which consist of a single hello.m file, the command to compile the executable is:
clang -framework Foundation hello.m -o hello Then you can run it:
./hello
This will output:
Hello World!
Nguồn: https://www.thuvienpdf.com