国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

IOS ObjectiveC中的賦值與對象拷貝

2020-07-26 02:40:21
字體:
來源:轉載
供稿:網友

IOS ObjectiveC中的賦值與對象拷貝

在開發過程中我們經常會遇到對象拷貝的問題,下面我們分別討論賦值操作、對象拷貝、以及淺拷貝(Shallow copy)與深拷貝(Deep copy)的區別與各自的實現方式。

一、不同對象的賦值操作

Objective-C中有兩類對象,一類是結構體(或者基本數據類型也算),另一類是NSObject對象。

對于結構體,代碼直接會操作其實體,因此賦值操作會創建一個源對象的副本(一個新的對象);而對于NSObject對象,必須使用指針來操作對象,所以其賦值操作相當于復制了指針,而非對象,也就是說賦值操作使得源指針和新指針都指向同一個NSObject對象。這樣講有些難以理解,請看下面的代碼:

// main.m  #import <Foundation/Foundation.h>  @interface TestObject : NSObject {   @public   int x;   int y; } @end  @implementation TestObject @end  typedef struct TestStruct {   int x;   int y; } TestStruct;  int main(int argc, const char * argv[]) {    @autoreleasepool {          TestStruct ts1 = {100, 50};     NSLog(@"ts1: %p, %d, %d", &ts1, ts1.x, ts1.y);          TestStruct ts2 = ts1;     NSLog(@"ts2: %p, %d, %d", &ts2, ts2.x, ts2.y);      TestObject* to1 = [[[TestObject alloc] init] autorelease];     NSLog(@"to1: %p, %d, %d", to1, to1->x, to1->y);          TestObject* to2 = to1;     NSLog(@"to2: %p, %d, %d", to2, to2->x, to2->y);        }   return 0; } 

程序的運行結果如下:

ts1: 0x7fff63463898, 100, 50 ts2: 0x7fff63463890, 100, 50 to1: 0x7fc342d00370, 0, 0 to2: 0x7fc342d00370, 0, 0 

程序代碼首先定義了一個類TestObject(繼承自NSObject),然后又定義了一個結構體TestStruct。這兩者都包含兩個整型的成員變量x和y。然后在main函數中,程序首先為TestStruct結構體ts1分配內存空間,并為其成員變量賦初值,x為100,y為50。然后通過NSLog函數打印出該結構體的地址和成員變量的值,即輸出的第一行內容。接著,程序執行了賦值語句,將ts1賦值給另一個TestStruct結構體對象ts2,這條語句會為ts2分配另一塊內存,然后把ts1的每個成員變量的值復制過來。第二行輸出也可以看出來,地址不一樣了,所以如果修改ts1的成員變量的值,是不會影響ts2的。

接著再來看TestObject。程序接著使用alloc靜態方法分配了一塊新的內存空間,然后通過init實例方法進行初始化(所有成員變量的值為0),最后將該內存空間的首地址返回。to1的實質就是一個指針,指向創建的TestObject對象。接著,程序將to1賦值給to2。to2也是一個指向TestObject對象的指針,其值與to1一樣,即兩者都指向同一個對象。所以在這種情況下,對to1的修改會同時影響to2。

二、對象拷貝

Foundation框架的NSObject類提供了兩個方法,分別是copy和mutableCopy方法,用于對NSObject對象進行拷貝操作。copy方法會調用NSCopying協議的copyWithZone:方法,而mutableCopy會調用 NSMutableCopying協議的mutableCopyWithZone:方法。將上面的代碼修改如下:

#import <Foundation/Foundation.h>  @interface TestObject : NSObject {   @public   int x;   int y; } @end  @implementation TestObject - (NSString*)description {   return [NSString stringWithFormat:@"%@: %p, x: %d, y: %d", [self class], self, x, y]; } @end  typedef struct TestStruct {   int x;   int y; } TestStruct;  int main(int argc, const char * argv[]) {   @autoreleasepool   {         TestObject* to1 = [[[TestObject alloc] init] autorelease];     to1->x = 100; to1->y = 50;     TestObject* to2 = [[[TestObject alloc] init] autorelease];     to2->x = 200; to2->y = 400;     TestObject* to3 = [[[TestObject alloc] init] autorelease];     to3->x = 300; to3->y = 500;          //創建包含to1, to2, to3的數組array1     NSArray* array1 = [NSArray arrayWithObjects:to1, to2, to3, nil];     NSLog(@"array1: %p, /n%@", array1, array1);          //array2是array1調用copy的結果     NSArray* array2 = [array1 copy];     NSLog(@"array2: %p, /n%@", array2, array2);     [array2 release];          //mutableArray2是array1調用mutableCopy的結果     NSMutableArray* mutableArray2 = [array1 mutableCopy];     NSLog(@"mutableArray2: %@, %p, /n%@", [mutableArray2 class], mutableArray2, mutableArray2);     [mutableArray2 removeLastObject];          NSLog(@"After remove last object of mutableArray2");          NSLog(@"array1: %p, /n%@", array1, array1);     NSLog(@"array2: %p, /n%@", array2, array2);     NSLog(@"mutableArray2: %p, /n%@", mutableArray2, mutableArray2);          //mutableArray3是mutableArray2調用mutableCopy的結果     NSMutableArray* mutableArray3 = [mutableArray2 mutableCopy];     NSLog(@"mutableArray3: %p, /n%@", mutableArray3, mutableArray3);     [mutableArray2 release];          //array4是mutableArray3調用copy的結果     NSArray* array4 = [mutableArray3 copy];     NSLog(@"array4: %@, %p, /n%@", [array4 class], array4, array4);     [mutableArray3 release];     [array4 release];   }   return 0; } 

程序的運行結果如下:

2012-03-22 19:20:49.548 ObjectCopy[18042:403] array1: 0x7f9071414820,  (   "TestObject: 0x7f90714141b0, x: 100, y: 50",   "TestObject: 0x7f90714141c0, x: 200, y: 400",   "TestObject: 0x7f9071414230, x: 300, y: 500" ) 2012-03-22 19:20:49.550 ObjectCopy[18042:403] array2: 0x7f9071414820,  (   "TestObject: 0x7f90714141b0, x: 100, y: 50",   "TestObject: 0x7f90714141c0, x: 200, y: 400",   "TestObject: 0x7f9071414230, x: 300, y: 500" ) 2012-03-22 19:20:49.551 ObjectCopy[18042:403] mutableArray2: __NSArrayM, 0x7f9072800000,  (   "TestObject: 0x7f90714141b0, x: 100, y: 50",   "TestObject: 0x7f90714141c0, x: 200, y: 400",   "TestObject: 0x7f9071414230, x: 300, y: 500" ) 2012-03-22 19:20:49.552 ObjectCopy[18042:403] After remove last object of mutableArray2 2012-03-22 19:20:49.552 ObjectCopy[18042:403] array1: 0x7f9071414820,  (   "TestObject: 0x7f90714141b0, x: 100, y: 50",   "TestObject: 0x7f90714141c0, x: 200, y: 400",   "TestObject: 0x7f9071414230, x: 300, y: 500" ) 2012-03-22 19:20:49.553 ObjectCopy[18042:403] array2: 0x7f9071414820,  (   "TestObject: 0x7f90714141b0, x: 100, y: 50",   "TestObject: 0x7f90714141c0, x: 200, y: 400",   "TestObject: 0x7f9071414230, x: 300, y: 500" ) 2012-03-22 19:20:49.553 ObjectCopy[18042:403] mutableArray2: 0x7f9072800000,  (   "TestObject: 0x7f90714141b0, x: 100, y: 50",   "TestObject: 0x7f90714141c0, x: 200, y: 400" ) 2012-03-22 19:20:49.557 ObjectCopy[18042:403] mutableArray3: 0x7f90729000d0,  (   "TestObject: 0x7f90714141b0, x: 100, y: 50",   "TestObject: 0x7f90714141c0, x: 200, y: 400" ) 2012-03-22 19:20:49.558 ObjectCopy[18042:403] array4: __NSArrayI, 0x7f9071416e70,  (   "TestObject: 0x7f90714141b0, x: 100, y: 50",   "TestObject: 0x7f90714141c0, x: 200, y: 400" ) 

程序的運行結果有幾點值得注意,首先是array1與array2的地址相同,因為NSArray對象在創建之后是不可以修改的。其次,NSArray的mutableCopy方法會返回一個NSMutableArray對象。第三,對于NSArray或者NSMutableArray來說,mutableCopy方法會創建新的可變數組對象,但其每個數組成員的值僅僅是原數組的一個指針賦值,這就是淺拷貝。而與之相對的則是深拷貝,即復制數組時不是復制數組每個元素的引用,而是創建一個與之相同的新對象。

第四,在NSArray對象上調用mutableCopy方法返回一個NSMutableArray對象,而在NSMutableArray對象上調用copy方法則返回一個NSArray對象,而不是NSMutableArray對象。

當然,以上討論的是Foundation框架中的NSArray與NSMutableArray類,如果想要實現對自己創建的類的對象進行拷貝,則需要讓類實現NSCopying協議。

如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 博乐市| 耒阳市| 正阳县| 雅江县| 奇台县| 吉木萨尔县| 沙坪坝区| 鹤庆县| 呼和浩特市| 永川市| 宝兴县| 灵武市| 娱乐| 德安县| 漳州市| 阆中市| 红安县| 烟台市| 金乡县| 徐闻县| 大连市| 巴南区| 布尔津县| 秭归县| 青河县| 日喀则市| 南城县| 德州市| 三门县| 寿光市| 建瓯市| 和龙市| 宣武区| 新和县| 靖远县| 镇平县| 武功县| 舒兰市| 金华市| 云安县| 鹤峰县|