Dark Horse Programmer--Simple OC Program

Source: Internet
Author: User

Knowledge points

1. #import的用途:

1>, like # include, copies the contents of a file

2> can automatically prevent the contents of a file from being copied repeatedly

2. #import <Foundation/NSObjCRuntime.h>

Declaration of nslog function in NSObjCRuntime.h

Path to 3.Foundation frame header file

1> Right-click Xcode.app-Show Package Contents

2> xcode.app/contents/developer/platforms/iphoneos.platform/developer/sdks/iphoneos6.0.sdk/system/library/ Frameworks/foundation.framework

4. main header file

1> main header file: The most important header file, the name is usually the same as the frame name, including all other header files in the frame

The primary header file name for the 2> Foundation framework is Foundation.h

3> only need to contain the foundation framework Master header file, you can use the whole framework of things

5. Operation Process

1> writing OC source files:. m,. C

2> compilation: Cc-c xxx.m xxx.c

3> Link: cc XXX.O xxx.o-framework Foundation

(Only the foundation framework is used to add the-framework Foundation)

4> run:./a.out

 1, OC introduction ? On the basis of the C language , a minimal object-oriented syntax is added. ? fully compatible with C language ? You can mix C code , even C + + code , in OC code ? applications that can use OC to develop Mac OS X platform and IOS platform

2. OC Syntax preview 1. Keywords? basically all keywords start with @ ? Here are some common keywords , take a look at the line , do not go through the data to find their meaning

@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 starts with @

For example @"Hello" is a string in OC , and "Hello" is a string in C language

3. Other Syntax

3, the development process of OC program

Similar to the C language :

4. The first 1 OC procedure 1. Code Writing

c language, oc The entrance of the program is still main function except to write a .m file such as here is a main.m file ( Span class= "S27" and file name can be Chinese )

1 #include <stdio.h>23//  OC Program entry: main function 4int Main () 5 {6     printf (" 1th oc program \ n"); 7     return 0 ; 8 }

2. Terminal Instructions ? compile cc –c main.m ? link cc main.o ? run ./a.out

5. The first 2 OC procedure 1. Code Writing

A bit different from C, using the NSLog function to output content

1 #import <Foundation/Foundation.h>23int  main ()4{  5     //  NSLog output will be automatically wrapped 6     NSLog (@ " 2nd OC Program!!! "); 7     8     return 0 ; 9 }

2. Terminal Instructions ? compile cc –c main.m ? link cc main.o –framework Foundation ? run ./a.out

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 #import ? As with #include , to copy the contents of a file ? can automatically prevent the file content to be copied multiple times, it is not necessary to add the following preprocessing instructions in the header file

#ifndef ? _stdio_h_

#define? _stdio_h_

#endif

5. Role of the Foundation framework ? Develop The necessary framework for OC, IOS, MAC programs ? This framework contains a number of commonly used APIs(application programming interfaces) ? 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);

1#import <Foundation/Foundation.h>2 3 4 BOOL Test (bool Mybool)5 {6     returnNO;7 }8 9 intMain ()Ten { OneBOOL B =YES; A      -BOOL B2 =NO; -      theBOOL B3 =1;//YES -      -BOOL B4 =0;//NO -      +      -     //NSLog (@ "%i", b); +      ANSLog (@"%d", Test (YES)); at     return 0; -}

6. The first 3 OC procedure 1. Development of multiple. m files

is the same as the development of multiple. c Files in C language

1) write 3 files ? main.m

#import "One.h"

int Main ()

{

test();

return 0;

}

? one.h

void test ();

? ONE.M

#import <Foundation/Foundation.h>

void Test ()

{

NSLog(@ " call the test function ");

}

2) terminal Instructions ? compile:cc –c main.m test.m ? Link:cc main.o TEST.O –framework Foundation ? run:./a.out

2. . M file and . C File Mix development 1) write 3 files ? main.m

#import "One.h"

int Main ()

{

test();

return 0;

}

? one.h

void test ();

? one.c

#include <stdio.h>

void Test ()

{

printf(" call the test function \ n");

}

2) terminal Instructions ? compile:cc –c main.m test.m ? Link:cc main.o TEST.O ? run:./a.out

( no-framework Foundation is used without the foundation Framework )

Dark Horse Programmer--Simple OC Program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.