IOS6學習筆記(一)

一.ARChtml

1.ARC環境下可使用-(void)dealloc{};處理一些事情(好比移除KVO觀察),但不要調用[super dealloc];數組

2.ARC與非ARC混編要注意符合Cocoa命名約定,好比不能用copyRight這樣的方法名,需改爲copyright。或使用NS_RETURNS_RETAINED或NS_RETURNS_NOT_RETAINED告訴編譯器使用哪一種內存管理規則,這些修飾符在NSObjCRuntime.h中定義的。安全

3.readonlyString在類擴展部分被從新定義時使用readwrite關鍵字進行修飾,這樣一來便爲該屬性建立了一個私有的設置方法(setter)。app

@interface ViewController : UIViewController函數

@property (nonatomic, readonly, strong) NSString *readonlyString;atom

@endspa

@interfaceViewController ()線程

@property (nonatomic, readwrite, strong) NSString *readonlyString;code

@endorm

二.屬性

1.原子性(atomic),LLVM4中加入了該特性。它本意指屬性的存取方法是線程安全的。好比,聲明瞭一個MSMutableArray類型的原子屬性stuff,這種狀況下self.stuff和self.stuff=otherStuff都是線程安全的。可是,使用objectAtIndex:方法訪問數組並非線程安全的,須要用鎖來保證線程安全。下面的代碼簡單表示了atomic的實現:

[_propertyLock lock];

id result = [[value retain] autorelease];

[_propertyLock unlock];

return result;

2.copy,對於非可變類(好比NSString、NSArray)的屬性一般應該使用copy修飾。用於聲明屬性的類可能會擁有可變的子類,好比NSString就可能有一個可變的子類NSMutableString,可以把NSMutableString對象賦給NSString屬性,這種狀況下你的屬性所指向的對象就有可能被其餘調用者改變。

@property (nonatomic, copy) NSMutableString *strongStr;

    NSMutableString *mString = [[NSMutableStringalloc] initWithFormat:@"mutableString"];

    self.strongStr = mString;

    NSLog(@"self.strongStr%@",self.strongStr);

    NSLog(@"mString’p%p",mString);

    NSLog(@"self.strongStr’p%p",self.strongStr);

    [mString appendString:@"."];

    NSLog(@"self.strongStr%@",self.strongStr);

    NSLog(@"mString’p%p",mString);

    NSLog(@"self.strongStr%p",self.strongStr);

輸出:

2013-06-21 18:08:50.954 PropertyTest[7467:907] self.strongStrmutableString

2013-06-21 18:08:50.957 PropertyTest[7467:907] mString’p0x1f592bb0

2013-06-21 18:08:50.959 PropertyTest[7467:907] self.strongStr’p0x1f592d30

2013-06-21 18:08:50.960 PropertyTest[7467:907] self.strongStrmutableString

2013-06-21 18:08:50.961 PropertyTest[7467:907] mString’p0x1f592bb0

2013-06-21 18:08:50.962 PropertyTest[7467:907] self.strongStr’p0x1f592d30

 

改成

@property (nonatomic, strong) NSMutableString *strongStr;

輸出:

2013-06-21 18:12:35.615 PropertyTest[7505:907] self.strongStrmutableString

2013-06-21 18:12:35.619 PropertyTest[7505:907] mString’p0x1e5a7220

2013-06-21 18:12:35.620 PropertyTest[7505:907] self.strongStr’p0x1e5a7220

2013-06-21 18:12:35.621 PropertyTest[7505:907] self.strongStrmutableString.

2013-06-21 18:12:35.622 PropertyTest[7505:907] mString’p0x1e5a7220

2013-06-21 18:12:35.623 PropertyTest[7505:907] self.strongStr0x1e5a7220


三.+(void)load方法

在第一次加載分類的時候執行。load方法是運行時的一個特性,每個分類均可以實現+load,全部的+load實現都會執行,但執行的順序是沒法保證的。還有,不要嘗試手動調用+load。一般在main函數以前(在類被加載到運行時的時候)就被調用。

Foundation Tool示例代碼

#import "LoadClass.h"

 

@implementation LoadClass

+ (void)load

{

    NSLog(@"loading ");

}

@end

 

@implementation LoadClass (category)

+(void)load

{

    NSLog(@"Category loading");

}

@end

 

@implementation LoadClassSubClass

 

+(void)load

{

    NSLog(@"subClass loading");

}

@end


其輸出爲:

2013-06-24 13:48:42.484 PropertyTest[1340:c07] loading 

2013-06-24 13:48:42.489 PropertyTest[1340:c07] subClass loading

2013-06-24 13:48:42.491 PropertyTest[1340:c07] Category loading


+(void)initialize方法

+initialize方法與其餘類方法機制相同,這意味着Category中的+initialize方法會覆蓋類自己的+initialize方法。而另外一個後果就是,若是父類中實現了+initialize方法,而子類中沒有重寫此方法,則子類初始化時就會使用父類的方法。這與「每一個類(全部父類和子類)只會執行一次+initialize方法」並不衝突,由於第二次調用父類的+initialize方法是對子類執行,但使用的是同一個方法實現而已。

#import 

@interface Abstract : NSObject
@end

@implementation Abstract

+ (void)initialize
{
    NSLog(@"%@ initialize",self);
}
@end

@interface Sub : Abstract
@end

@implementation Sub
@end

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        NSLog(@"main Start");
        [Sub class];
        NSLog(@"Hello, World!");

    }
    return 0;
}

其輸出爲:

main Start
Abstract initialize
Sub initialize
Hello, World!

若是不想子類使用父類的+initialize方法,則父類的+initialize方法能夠這樣寫

+ (void)initialize
    {
        if(self == [WhateverClass class])
        {
            ...perform initialization...
        }
    }

另一些,不能這樣寫的情形有:

  • 須要在每一個子類初始化時執行相同的+initialize方法,好比使用+initialize記錄每一個繼承子類的名字。
  • 使用KVO通知的狀況,KVO機制的實現機制是建立動態子類(creates dynamic subclasses),若是使用上述代碼包含,則在動態建立的子類中則不會調用+initialize實現。
相關文章
相關標籤/搜索