Example one (similar to c)
//1. Code Writing//like the C language, the entrance to the OC program is still the main function, except that it is written in a. m file. For example, here is a main.m file (the file name can be Chinese)#include <stdio.h>intMain () {printf ("Hello world\n"); return 0;}//2. Terminal InstructionsCc-c main.m//compilingCC MAIN.O//links./A. out //Run
Example two (different C)
//1. Code Writing//Unlike the C language, use the NSLog function to output content#import<Foundation/Foundation.h>intMain () {NSLog (@"Hello World"); return 0;}//2. Terminal InstructionsCc-c main.m//compilingCC Main.o-framework Foundation//links./A. out //Run
3. The difference between NSLog and printf
NSLog receives the OC string as a parameter, printf receives the C language string as a parameter
NSLog after output, does not wrap automatically after printf output
#import <Foundation/Foundation.h> required to use NSLog
Using printf requires # include <stdio.h>
4. The role of #inprot
Same as # include, used to copy the contents of a file
It is possible to automatically prevent the file contents from being copied multiple times, which means that the following preprocessing instructions are not included in the header file.
#ifndef _stdio_h_
#define _stdio_h_
#endif
The role of the 5.F oundation framework
Development of OC, IOS, MAC program necessary framework
This framework contains a number of common APIs
The framework contains a lot of header files , if you want to use the contents of the entire framework , including its main header file can be
#import <Foundation/Foundation.h>
6. use of BOOL
the nature of the BOOL type
typedef signed char BOOL;
There are 2 values for variables of type BOOL : YES/NO
#define YES (BOOL) 1
#define NO (BOOL) 0
output of BOOL (as integer )
NSLog (@ "%d%d", YES, NO);
Example three (multi-file development)
1. Development of multiple. m files
//is the same as the development of multiple. c files in C language//Write 3 filesmain.m#import "one.h"intMain () {test (); return 0;} One.hvoidtest (); ONE.M#import<Foundation/Foundation.h>voidTest () {NSLog (@"the test function was called");}//Terminal InstructionsCc-c main.m TEST.M//compilingCC MAIN.O Test.o-framework Foundation//links./A. out //Run
2..M file and. c File Hybrid development
//Write 3 filesmain.m#import "one.h"intMain () {test (); return 0;} One.hvoidtest (); One.c#include<stdio.h>voidTest () {printf ("the test function was called \ n");} Terminal instruction CC-C MAIN.M TEST.M//compilingCC MAIN.O TEST.O//link does not use the foundation framework, you do not have to-framework Foundation./A. out //Run
OBJECTIVE-C First Small program