《Programming with Objective-C》第三章 Working with Objects

來源:互聯網
上載者:User

標籤:

Object和普通變數的區別

If you’re used to using terms like the stack and the heap, a local variable is allocated on the stack, while objects are allocated on the heap.

- (void)f{    int a = 2;                    //Stack    NSString *s = @"Hello";        //Heap}

  函數f中,a指向的記憶體在棧中,函數退出的時候變數a將不能再被訪問,其記憶體也會被釋放;s指向的記憶體在堆中,函數退出的時候s也不能再被訪問,但是s指向的記憶體可能繼續存在。

 

Factory Method v.s. Abstract Factory

 Todo 等尋找資料後再補上

Objective-C是一門動態語言
id someObject = @"Hello, World!";[someObject removeAllObjects];

  編譯時間,someObject是一個id類型,所以編譯器不會報錯。
  運行時,編譯器會出現Runtime Error因為NSString對象不能響應removeAllObjects.

NSString *someObject = @"Hello, World!";[someObject removeAllObjects];

  編譯時間,編譯器知道someObject是一個NSString類型,NSString對象不能響應removeAllObjects,所以編譯時間編譯器會報錯

 

等於/不等於
//基本類型if (someInteger == 42) {    // someInteger has the value 42}//比較是否是同一個對象if (firstPerson == secondPerson)    {    // firstPerson is the same object as secondPerson}//比較2個對象的內容是否相等if ([firstPerson isEqual:secondPerson])     {    // firstPerson is identical to secondPerson}//NSNumber, NSString and NSDate等類型比較大小不能用>、<,應該用compare:方法if ([someDate compare:anotherDate] == NSOrderedAscending) {    // someDate is earlier than anotherDate}

 

《Programming with Objective-C》第三章 Working with Objects

聯繫我們

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