First, OBJECTIVE-C (hereinafter referred to as OC) Introduction
Objective-c is based on C, the addition of object-oriented features to expand the general-purpose high-level programming language, but also writing OBJC, obj-c and less used Objective C. Objective-c is primarily used for Mac OS X and IOS systems and their associated APIs such as Cocoa, Cocoa Touch. Objective-c originally originated from the NeXTSTEP system, which was then inherited by OS X and IOS. Currently the main supported compilers are GCC and Clang.
On the basis of the C language, a minimum of object-oriented syntax is added, which is fully compatible with C language, can be mixed with C code in OC Code, even C + + code, can use OC to develop Mac OS x platform and iOS platform application;
Ii. Overview of the grammar
1. Keywords
Basically all keywords start with @
Some common keywords are listed below:
@interface、@implementation、@end
@public、@protected、@private、@selector
@try、@catch、@throw、@finally
@protocol、@optional、@required、@class
@property、@synthesize、@dynamic
self、super、id、_cmd、__block、__strong、__weak、
2. String begins with @
For example @ "Hello" is a string in OC, and "Hello" is a string in C language
3, other syntax
Third, the implementation process of OC Program
Four, write the OC code, experience the charm of OC
Like the C language, the entry for the OC program is still the main function, except that it is written to a. m file. For example, this is written in a main.m file (the file name can be Chinese)
<span style = "font-size: 14px;"> # include <stdio.h>
int main ()
{
printf ("The first OC program \ n");
return 0;
}
</ span>
Compiling cc–c main.m
Link CC MAIN.O
Run./a.out
A bit different from C, using the nslog function to output content
<span style = "font-size: 14px;"> # import <Foundation / Foundation.h>
int main ()
{
NSLog (@ "2nd OC program");
return 0;
}
</ span>
Compiling cc–c main.m
Link cc main.o –framework Foundation
Run./a.out
1. The difference between NSLog and printf
1) NSLog receives the OC string as a parameter, printf receives the C language string as a parameter
2) NSLog after output, does not wrap automatically after printf output.
3) #import <Foundation/Foundation.h> required to use NSLog
4) Use printf requires # include <stdio.h>
2. #import的作用
1) to copy the contents of a file, like # include
2) can automatically prevent the file content is copied multiple times, it is not necessary to add the following preprocessing instructions in the header file
#ifndef _stdio_h_#define _stdio_h_#endif
3. Role of the foundation framework
1) Develop the necessary framework for OC, IOS and Mac programs
2) This framework contains a number of commonly used APIs (application programming interfaces)
3) 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>
4. Use of bool
the nature of the bool type
<span style= "FONT-SIZE:14PX;" >typedef signed charbool;</span>
There are 2 values for variables of type bool: YES, NO
<span style="font-size:14px;">#import "one.h"
int main()
{
test();
return 0;
}
</span>
? Output of bool (As Integer)
NSLog (@ "%d%d", yes,no);
Note: OBJECT-C provides a bool type, but this bool type is not the same as in C + +: everything in C + + that is not 0 value
Is true, and a value of 0 is false. But Object-c 1 is true and is defined by the macro as yes,0 false and defined by the macro as NO.
five, multiple. m file compilation issues
1) Write 3 files
Main.m
<span style= "FONT-SIZE:14PX;" > #import "one.h" int main () { test (); return 0;} </span>
One.h
<span style= "FONT-SIZE:14PX;" >void Test ();</span>
ONE.M
<span style = "font-size: 14px;"> # import <Foundation / Foundation.h>
void test ()
{
NSLog (@ "Called the test function");
}
</ span>
2) Run the command to compile the execution:
Compilation: Cc–c main.m TEST.M
Link: cc MAIN.O test.o–frameworkfoundation
Run:./a.out
Vi. mixed development of. m files and. c Files
1) Write 3 files
Main.m
<span style="font-size:14px;">#import "one.h"
int main()
{
test();
return 0;
}
</span>
One.h
<span style= "FONT-SIZE:14PX;" >void Test ();</span>
One.c
<span style = "font-size: 14px;"> # include <stdio.h>
void test ()
{
printf ("The test function was called \ n");
}
</ span>
2) Run the command, compile the execution
Compilation: Cc–c main.m TEST.M
Link: cc MAIN.O TEST.O
Run:./a.out
(No-framework Foundation is used without the foundation framework)
Black Horse Programmer------OBJECTIVE-C Language Overview and initial experience of grammar