當前位置:成語大全網 - 新華字典 - iOS 基礎知識點

iOS 基礎知識點

壹、屬性修飾符

讀寫屬性: (readwrite/readonly)

語義特性:(內存有關):(assign/retain/copy)

原子特性:(和線程安全有關): (atomicit/nonatomic)

readonly:只讀,屬性只會生成getter方法,不會生成setter.

readwrite:既可讀又可寫.屬性自動生成setter和getter方法.(默認的讀寫特性)

getter= 方法名:指定生成getter方法時的方法名.

setter= 方法名:指定生成setter方法時的方法名.

assign: 默認類型,setter方法直接賦值,而不進行retain操作,也可以只針對對象,只是做簡單的賦值操作. 默認的語義特性.

retain: 針對於對象類型,setter方法對參數進行release舊值,再retain新值。

copy: setter方法進行Copy操作,與retain壹樣, 針對於對象類型,會拷貝壹個新的對象,將新的對象的引用計數加1

nonatomic: 禁止多線程,變量保護,提高性能

atomic:原子特性,保證線程安全.系統默認的原子特性.

weak:指針指向的地址壹旦被釋放,這些指針都將被賦值為nil。這樣的好處能有效的防止野指針。消除循環引用 。

二、 @dynamic 關鍵字

@dynamic dynamicStr; // 告訴編譯器,不自定生成getter和setter方法,避免編譯期間產生警告,然後由自己實現存取方法。

三、 @synthesize 關鍵字

@synthesize strongStr = _strongStr; // 可以定義 與變量名不相同的getter和setter的命名,籍此來保護變量不會被不恰當的訪問, 編譯器期間,讓編譯器自動生成getter/setter方法 當有自定義的存或取方法時,自定義會屏蔽自動生成該方法

四、重定義setter方法getter方法

// 重定義setter方法

// 重定義getter方法

五、不可變字符串 NSString

// 運行log日誌:

2021-03-11 17:30:08.112340+0800 TestObject-C[93449:1623723] temp1: 不可變字符串, 0x10df9e778

2021-03-11 17:30:08.112529+0800 TestObject-C[93449:1623723] str: 不可變字符串, 0x10df9e778

2021-03-11 17:30:08.112610+0800 TestObject-C****[****93449:1623723****]**** strongStr: ****不可變字符串****, 0x10df9e778

六、可變字符串 NSMutableString

// 壹、將不可變字符串賦值給可變字符串對象。

// 賦值可變字符串對象後,對象仍然是不可變對象

2021-03-11 17:30:08.114159+0800 TestObject-C[93449:1623723] mTemp: 可變字符串1, 0x10df9e878

2021-03-11 17:30:08.114393+0800 TestObject-C[93449:1623723] mCopyStr: 可變字符串1, 0x10df9e878

2021-03-11 17:30:08.114697+0800 TestObject-C****[****93449:1623723****]**** mStrongStr: 可變字符串****1, 0x10df9e878

// 壹、將可變字符串賦值給可變字符串對象。

2021-03-11 19:38:34.493763+0800 TestObject-C[95274:1718783] mTemp2: 可變字符串2, 0x600002710ea0, __NSCFString

2021-03-11 19:38:34.493923+0800 TestObject-C[95274:1718783] mCopyStr: 可變字符串2, 0x6000027110e0, __NSCFString

2021-03-11 19:38:34.494037+0800 TestObject-C****[****95274:1718783****]**** mStrongStr: ****可變字符串****2, 0x600002710ea0, __NSCFString

2021-03-11 19:38:34.494422+0800 TestObject-C[95274:1718783] mTemp2: 可變字符串211, 0x600002710ea0, __NSCFString

2021-03-11 19:38:34.494697+0800 TestObject-C[95274:1718783] mCopyStr: 可變字符串2, 0x6000027110e0, __NSCFString

2021-03-11 19:38:34.494903+0800 TestObject-C****[****95274:1718783****]**** mStrongStr: ****可變字符串****211, 0x600002710ea0, __NSCFString

七、富文本字符串 NSMutableAttributedString

2021-03-11 19:34:52.041131+0800 TestObject-C[95207:1715249] attriStr: 我是富文本我是富文本我是富文本我是富文本, 0x600000a5bc00, NSConcreteMutableAttributedString

2021-03-11 19:34:52.041307+0800 TestObject-C[95207:1715249] mutableAttri: 我是富文本我是富文本我是富文本我是富文本, 0x600000a5be40, NSConcreteAttributedString

2021-03-11 19:34:52.041425+0800 TestObject-C****[****95207:1715249****]**** mutableStrongAttri: ****我是富文本我是富文本我是富文本我是富文本****, 0x600000a5bc00, NSConcreteMutableAttributedString

2021-03-11 19:34:52.041943+0800 TestObject-C[95207:1715249] attriStr: 我是富文本我是富文本我是富文本我是富文本---我是新添加的, 0x600000a5bc00, NSConcreteMutableAttributedString

2021-03-11 19:34:52.042281+0800 TestObject-C****[****95207:1715249****]**** mutableStrongAttri: ****我是富文本我是富文本我是富文本我是富文本****---****我是新添加的****, 0x600000a5bc00, NSConcreteMutableAttributedString

八、不可變數組NSArray

//壹、 不可變數組賦值NSArray的對象

2021-03-11 19:04:42.636207+0800 TestObject-C[94739:1689350] arr: (1, 2,

3), 0x600001d5c8d0

2021-03-11 19:04:42.636411+0800 TestObject-C[94739:1689350] persons: ( 1, 2, 3), 0x600001d5c8d0

2021-03-11 19:04:42.636538+0800 TestObject-C[94739:1689350] students: (1, 2, 3), 0x600001d5c8d0

2021-03-11 19:04:42.636646+0800 TestObject-C[94739:1689350] arr: (1, 2, 3), 0x600001d5c8d0

2021-03-11 19:04:42.636767+0800 TestObject-C[94739:1689350] students: (1, 2,3), 0x600001d5c8d0, __NSArrayI

// 二、不可變數組賦值給NSMutableArray的對象

**2021-03-11 19:15:59.046728+0800 TestObject-C[94934:1699039] arr: (1,2,3), 0x600002f6c000

2021-03-11 19:15:59.046878+0800 TestObject-C[94934:1699039] cars: (1, 2,3), 0x600002f6c000, __NSArrayI2021-03-11 19:15:59.046982+0800 TestObject-C[94934:1699039] dogs: (1,2,3), 0x600002f6c000, __NSArrayI

九、可變數組 NSMutableArray

2021-03-11 19:22:39.183745+0800 TestObject-C[95044:1705082] arr2: (2,4,10,12), 0x600000fefea0

2021-03-11 19:22:39.184067+0800 TestObject-C[95044:1705082] cars: (2,4,10), 0x600000fef240, __NSArrayI

**2021-03-11 19:22:39.184272+0800 TestObject-C[95044:1705082] dogs: (2,4,10,12), 0x600000fefea0, __NSArrayM

十、不可變字典 NSDictionary

// 不可變數組賦值給NSDictionary對象

2021-03-11 19:25:32.789615+0800 TestObject-C[95102:1708378] dic: {key1 = 1;key2 = value;}, 0x6000028799c0**

2021-03-11 19:25:32.789801+0800 TestObject-C[95102:1708378] personDic: { key1 = 1;key2 = value;}, 0x6000028799c0, __NSDictionaryI

2021-03-11 19:25:32.789919+0800 TestObject-C[95102:1708378] studentDic: { key1 = 1;key2 = value;}, 0x6000028799c0, __NSDictionaryI

十壹、可變字典 NSMutableDictionary

2021-03-11 19:59:28.739545+0800 TestObject-C[96373:1735182] mDic: { key1 = 10;key2 = value2;}, 0x6000017f0700, __NSDictionaryM

**2021-03-11 19:59:28.740177+0800 TestObject-C[96373:1735182] carDic: {key1 = 10;key2 = value2;}, 0x6000017f07a0, __NSFrozenDictionaryM

2021-03-11 19:59:28.740359+0800 TestObject-C[96373:1735182] dogDic: { key1 = 10;key2 = value2;}, 0x6000017f0700, __NSDictionaryM

2021-03-11 20:05:24.028840+0800 TestObject-C[96469:1740141] mDic: {key1 = 10; key2 = value2;key4 = value4;}, 0x6000011bd7e0, __NSDictionaryM

2021-03-11 20:05:24.028930+0800 TestObject-C[96469:1740141] carDic: { key1 = 10; key2 = value2;}, 0x6000011bd820, __NSFrozenDictionaryM

2021-03-11 20:05:24.029028+0800 TestObject-C[96469:1740141] dogDic: {key1 = 10;key2 = value2;key4 = value4;}, 0x6000011bd7e0, __NSDictionaryM

十二、不可變集合 NSSet

// 壹、不可變NSSet賦值給 NSSet對象

2021-03-11 20:09:49.418694+0800 TestObject-C[96545:1744249] set: {( 1,2,3)}, 0x600003fd2e80, __NSSetI

2021-03-11 20:09:49.418826+0800 TestObject-C[96545:1744249] personSet: {(1,2,3)}, 0x600003fd2e80, __NSSetI

2021-03-11 20:09:49.418914+0800 TestObject-C[96545:1744249] studentSet: {(1,2,3)}, 0x600003fd2e80, __NSSetI

// 二、不可變NSSet賦值給 NSMutableSet對象

2021-03-11 20:09:49.419281+0800 TestObject-C[96545:1744249] carSet: {(1,2,3)}, 0x600003fd2e80, __NSSetI

2021-03-11 20:09:49.419362+0800 TestObject-C[96545:1744249] dogSet: {(1, 2,3)}, 0x600003fd2e80, __NSSetI

十三、可變集合 NSMutableSet

2021-03-11 20:18:06.896997+0800 TestObject-C[96705:1752827] mSet: {(3,1,2,5)}, 0x600001e0a700, __NSSetM

2021-03-11 20:18:06.897218+0800 TestObject-C[96705:1752827] carSet: {( 1,2,3)}, 0x600001014bd0, __NSSetI

2021-03-11 20:18:06.897340+0800 TestObject-C[96705:1752827] dogSet: {(3,1,2, 5)}, 0x600001e0a700, __NSSetM