Learn Objectvie-C on the Mac 2nd Edition 筆記

來源:互聯網
上載者:User

標籤:style   blog   color   io   os   ar   使用   for   strong   

Chapter 1
Apple’s Cocoa (for OS X) 和 Cocoa Touch (for iOS) toolkits 都是用 Objective-C寫的.

Chapter 2

(1) .m 代表 “messages”的縮寫

  .m -> Object-C compiler  .c -> C compiler  .cpp -> C++ compiler

(2)在 Objective-C中, #import 保證了每個標頭檔只被包含一次,如同c中的#ifdef標頭檔衛士的作用一樣。

(3)NSLog()如同 C中的printf(),不過增加了時間和日期戳,並自動在行尾加了“\n"分行符號。

      @(" ")表示" "中的字串被當做一個NSString單元來處理。在NSLog中如果直接使用了C風格的字串”“而缺少@(),則在編譯時間得到一個warning,運行時會crash。

(4)BOOL 類型

     在Objectvie-C中,BOOL類型是實際上是一個signed char的typedef,因此不僅可以用YES(值=1)和NO(值=0)表示C中的true和false,而且可以存放其他的值。這裡會有一個易錯點:如果將一個short或int等類型的值賦給BOOL變數後,會截斷末尾的8位,如果這末尾8位值剛好為0(如8960,其十六進位值為 0X2300),此值的BOOL值就是0,即NO,而不是非NO了。

Chapter 3

(1)電腦科學的核心思想:Indirection。Instead of using a value dirctly in your code, use a pointer to the value; Instead of doing something by yourself, ask another person to do it.

(2)一個檔案讀取樣本:

#import <Foundation/Foundation.h>int main(int argc, const char * argv[]){    if(argc == 1)    {        NSLog (@"You need to provide a file Name");        return (1).    }    FILE *wordFile = fopen(argv[1], "r"); // argv[0] is the code file path    char word[100];    while(fgets(word, 100, wordFile)    {         // strip off the trailing \n         word[strlen(word) - 1] = ‘\0‘;         NSLog(@"%s kis %lu characters long", word, strlen(word));      }    fclose(wordFile);    return(0).} // main

(3)在Objective-C中,方法調用採用的是中綴標記法(infix notation),如

[circle setFillColor: kRedColor];

    如果一個方法無參數,則無冒號和參數列表;如果有參數,則有冒號:

- (void) scratchTheCat; // 無參數- (void) scratchTheCat: (CatType) critter; // 有參數

Chapter 6

 (1)Objective-C類的代碼可以分為2個部分:

          1>. @interface 部分   

@interface XX : YY{    variables;} public method declarations.@end

  提供了該類的公用介面聲明。

          2>. @implementation 部分   

@implementation XXprivate self-used methods@end

   提供了該類的實現以及內建函式。

          

Learn Objectvie-C on the Mac 2nd Edition 筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.