Programming with Objective-C 學習筆記

來源:互聯網
上載者:User

標籤:官方文檔   objetive-c   objective   oc   

這幾天把Programming with Objective-C再看了一邊,發現有很多以前不明確的地方。現在把一些重要的點記下來,鞏固學習效果。

1.Objective-C Classes Are also Objects(OC類也是對象)

In Objective-C, a class is itself an object with an opaque type called Class. Classes can’t have properties defined using the declaration syntax shown earlier for instances, but they can receive messages.
The typical use for a class method is as a factory method, which is an alternative to the object allocation and initialization procedure described in Objects Are Created Dynamically (page 34).
類也是特殊的對象,典型的用法是類中的方法被稱作Factory 方法。

2.Use Pointers to Keep Track of Objects(使用指標記錄對象)

Objects normally have a longer life than the simple scope of a method call. In particular, an object often needs to stay alive longer than the original variable that was created to keep track of it, so an object’s memory is allocated and deallocated dynamically.
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.
這裡針對 local variable(本地變數)和object(對象)的不同點,介紹了兩套解釋的術語。後面一種比較熟悉,本地變數存放在stack(棧)中,對象存放在heap(堆)中。

3.Objects Are Created Dynamically(對象是被動態建立的)

The NSObject root class provides a class method, alloc, which handles this process for you:

?+ (id)alloc;

Notice that the return type of this method is id. This is a special keyword used in Objective-C to mean “some kind of object.” It is a pointer to an object, like (NSObject *), but is special in that it doesn’t use an asterisk.
The alloc method has one other important task, which is to clear out the memory allocated for the object’s properties by setting them to zero. This avoids the usual problem of memory containing garbage from whatever was stored before, but is not enough to initialize an object completely.
建立對象時,需要調用alloc方法,返回的類型是id,id是一個只想對象的指標,它表示“某一類對象”,但比較特殊的是不需要加星號。
alloc方法另一個作用是講對象中的屬性歸零,這避免了記憶體垃圾產生的影響。
You need to combine a call to alloc with a call to init, another NSObject method:

- (id)init;

The init method is used by a class to make sure its properties have suitable initial values at creation, and is covered in more detail in the next chapter.
init方法用來確保所有參數都被初始化。

4.Objective-C Is a Dynamic Language(OC是一門動態語言)

Because of Objective-C’s dynamic nature, it doesn’t matter what specific class type you use for that pointer—the correct method will always be called on the relevant object when you send it a message.

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

OC是動態語言,這裡它舉了一個例子,someObject是NSString型對象,沒有removeAllObjects方法,但編譯器沒有報錯,只會在運行時拋出異常。這說明OC只在運行時確定方法的調用。

5.Most Properties Are Backed by Instance Variables(大部分屬性基於執行個體變數)

For a property called firstName, for example, the synthesized instance variable will be called _firstName.
In general, you should use accessor methods or dot syntax for property access even if you’re accessing an
object’s properties from within its own implementation, in which case you should use self:
The exception to this rule is when writing initialization, deallocation or custom accessor methods, as described later in this section.
比如屬性名稱為firstName,那麼綜合的執行個體變數名為_firstName
一般的,通過self.文法擷取屬性。
也有例外情況,當我們初始化,銷毀,或自訂getter/setter方法時,需要直接存取執行個體變數,如_firstName。

6.Designated Initializer(指定初始化器)

這部分還沒有深入學習。

7.Properties Are Atomic by Default(屬性預設是原子的)

Propertyatomicityisnotsynonymouswithanobject’sthreadsafety.
Consider an XYZPerson object in which both a person’s first and last names are changed using atomic accessors from one thread. If another thread accesses both names at the same time, the atomic getter methods will return complete strings (without crashing), but there’s no guarantee that those values will be the right names relative to each other. If the first name is accessed before the change, but the last name is accessed after the change, you’ll end up with an inconsistent, mismatched pair of names.
屬性設定為atomic不能保證安全執行緒,這裡舉了一個XYZPerson對象的例子。

8.Avoid Strong Reference Cycles(避免循環參考)

A common scenario is that the table view has a reference to its delegate and the delegate has a reference back to the table view, as shown in Figure 3-7 (page 59).

舉了一個tableview和delegate的例子說明為什麼會發生循環參考。
The way to solve this problem is to substitute one of the strong references for a weak reference. A weak reference does not imply ownership or responsibility between two objects, and does not keep an object alive.
為了避免迴圈應用,可以使用weak修飾符,如

 @property (weak) id delegate;
NSObject * __weak weakVariable;
9.向weak修飾的對象發送訊息的標準方法
NSObject *cachedObject = self.someWeakProperty; //1 if (cachedObject) { //2 [someObject doSomethingImportantWith:cachedObject]; //3 } //4 cachedObject = nil;
10.Copy Properties Maintain Their Own Copies(拷貝類型屬性維護自己的副本)

通過例子說明為什麼NSString一般使用copy修飾符。

11.Categories and Class Extension(分類和類擴充)

Categories can be used to declare either instance methods or class methods but are not usually suitable for declaring additional properties.
分類可以什麼類方法和執行個體方法,但不能聲明屬性。
Unlike regular categories, a class extension can add its own properties and instance variables to a class.
類擴充可以增加屬性和執行個體變數。
屬性:

@interface XYZPerson ()@property (readwrite) NSString *uniqueIdentifier;@end

執行個體變數:

@interface XYZPerson () {    id _someCustomInstanceVariable;}
12.Check that Optional Methods Are Implemented at Runtime(運行時檢查可選方法是否被實現)

If a method in a protocol is marked as optional, you must check whether an object implements that method before attempting to call it.

NSString *thisSegmentTitle;if ([self.dataSource respondsToSelector:@selector(titleForSegmentAtIndex:)]){    thisSegmentTitle = [self.dataSource       titleForSegmentAtIndex:index];}

當協議中有可選方法時,需要使用respondsToSelector:方法判斷是否實現了可選方法。

13.Objects Use Properties to Keep Track of Blocks(block作為屬性存到對象中)
property (copy) void (^blockProperty)(void);

You should specify copy as the property attribute, because a block needs to be copied to keep track of its captured state outside of the original scope. This isn’t something you need to worry about when using Automatic Reference Counting, as it will happen automatically, but it’s best practice for the property attribute to show the resultant behavior. For more information, see Blocks Programming Topics .
解釋了為什麼block屬性要用copy修飾。

14.Use __block Variables to Share Storage(使用__block修飾符共用記憶體地區)

If you need to be able to change the value of a captured variable from within a block, you can use the __block storage type modifier on the original variable declaration.

__block int anInteger = 42;void (^testBlock)(void) = ^{    NSLog(@"Integer is: %i", anInteger);};anInteger = 84;testBlock();

block在捕獲範圍外的變數時,預設copy變數的值。如果要改變修改變數,需要用__block修飾。

15.Avoid Strong Reference Cycles when Capturing self(block中調用self時避免循環參考)
- (void)configureBlock {    XYZBlockKeeper * __weak weakSelf = self;    self.block = ^{    [weakSelf doSomething];   // capture the weak reference} }

block中如果向self對象發送訊息,會發生循環參考,給出了標準的解決方案。

Programming with Objective-C 學習筆記

相關文章

聯繫我們

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