第一部分 1.0 OC語言的簡介,1.0oc

來源:互聯網
上載者:User

第一部分 1.0 OC語言的簡介,1.0oc

一、OC簡介

二、OC文法預覽
1.關鍵字

@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.字串以@開頭
比如@"Hello"是OC中的字串,而"Hello"則是C語言中的字串

其他文法

四、第1個OC程式
1.代碼編寫
跟C語言一樣,OC程式的入口依然是main函數,只不過寫到一個.m檔案中。比如這裡寫到一個main.m檔案中(檔案名稱可以是中文)

#include <stdio.h>int main(){    printf("第1個OC程式\n");    return 0;}

2.終端指令
編譯cc –c main.m
連結cc main.o
運行./a.out

五、第2個OC程式
1.代碼編寫
來點跟C語言不一樣的,使用NSLog函數輸出內容

#import <Foundation/Foundation.h>int main(){    NSLog(@"第2個OC程式");    return 0;}

2.終端指令
編譯cc –c main.m
連結cc main.o –framework Foundation
運行./a.out

3.NSLog與printf的區別
NSLog接收OC字串作為參數,printf接收C語言字串作為參數
NSLog輸出後會自動換行,printf輸出後不會自動換行
使用NSLog需要#import <Foundation/Foundation.h>
使用printf需要#include <stdio.h>

4.#import的作用
跟#include一樣,用來拷貝某個檔案的內容
可以自動防止檔案內容被拷貝多次,也就以為著標頭檔中不用加入下面的預先處理指令了
#ifndef     _STDIO_H_
#define    _STDIO_H_
#endif

5.Foundation架構的作用
開發OC、iOS、Mac程式必備的架構
此架構中包含了很多常用的API(應用編程介面)
架構中包含了很多標頭檔,若想使用整個架構的內容,包含它的主標頭檔即可
#import <Foundation/Foundation.h>

6.BOOL的使用
BOOL類型的本質
typedef signed char BOOL;
BOOL類型的變數有2種取值:YES、NO
#define YES (BOOL)1
#define NO  (BOOL)0
BOOL的輸出(當做整數來用)
NSLog(@"%d %d", YES, NO);

六、第3個OC程式
1.多個.m檔案的開發
跟C語言中多個.c檔案的開發是一樣的
1.0.編寫3個檔案
main.m

#import "one.h"int main(){    test();    return 0;}

one.h

void test();

one.m

#import <Foundation/Foundation.h>void test(){    NSLog(@"調用了test函數");}

1.1.終端指令
編譯:cc –c main.m test.m
連結:cc main.o test.o –framework Foundation
運行:./a.out

2. .m檔案和.c檔案混用開發
2.0編寫3個檔案
main.m
#import "one.h"
int main()
{
    test();
    return 0;
}
one.h
void test();
one.c
#include <stdio.h>
void test()
{
    printf("調用了test函數\n");
}

2.1終端指令
編譯:cc –c main.m test.m
連結:cc main.o test.o
運行:./a.out
(沒有使用Foundation架構的話,就不用-framework Foundation)

    

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.