OBJECTIVE-C First Small program

Source: Internet
Author: User

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

Related Article

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.