標籤:
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