实现<NSCopying>协议

作者:追风剑情 发布于:2019-3-7 13:35 分类:Objective-C

示例

  1. #import <Foundation/Foundation.h>
  2.  
  3. // 实现不可变副本、可变副本协议
  4. @interface ClassA : NSObject <NSCopying, NSMutableCopying>
  5. @property int age;
  6. // 注意:这里使用了copy特性
  7. @property (nonatomic, copy) NSString *theName;
  8.  
  9. -(id) copyWithZone:(NSZone *)zone;
  10. -(id) mutableCopyWithZone:(NSZone *)zone;
  11. @end
  12.  
  13. @implementation ClassA
  14. @synthesize age;
  15. @synthesize theName;
  16. -(id) copyWithZone:(NSZone *)zone
  17. {
  18. NSLog(@"ClassA copyWithZone");
  19. // 在指定存储区(zone)中分配内存
  20. //ClassA *newClassA = [[ClassA allocWithZone: zone] init];
  21. // 写成[self class]方便子类调用
  22. ClassA *newClassA = [[[self class] allocWithZone: zone] init];
  23. newClassA.age = age;
  24. // 因为用了copy特性,setter会生成副本[theName copy];
  25. newClassA.theName = theName;
  26. return newClassA;
  27. }
  28.  
  29. -(id) mutableCopyWithZone:(NSZone *)zone
  30. {
  31. NSLog(@"ClassA mutableCopyWithZone");
  32. // 在指定存储区(zone)中分配内存
  33. //ClassA *newClassA = [[ClassA allocWithZone: zone] init];
  34. // 写成[self class]方便子类调用
  35. ClassA *newClassA = [[[self class] allocWithZone: zone] init];
  36. newClassA.age = age;
  37. // 因为用了copy特性,setter会生成副本[theName copy];
  38. newClassA.theName = theName;
  39. return newClassA;
  40. }
  41. @end
  42.  
  43. @interface ClassB : ClassA
  44.  
  45. @end
  46.  
  47. @implementation ClassB
  48. -(id) copyWithZone:(NSZone *)zone
  49. {
  50. NSLog(@"ClassB copyWithZone");
  51. // 在指定存储区(zone)中分配内存
  52. // [self class] 确保分配的是新的ClassB对象,而不是ClassA对象
  53. //ClassB *newClassB = [[[self class] allocWithZone: zone] init];
  54. //newClassB.age = self.age;
  55. // 考虑到父类可能已经实现了数据复制
  56. ClassB *newClassB = (ClassB *)[super copyWithZone: zone];
  57. return newClassB;
  58. }
  59.  
  60. -(id) mutableCopyWithZone:(NSZone *)zone
  61. {
  62. NSLog(@"ClassB mutableCopyWithZone");
  63. // 在指定存储区(zone)中分配内存
  64. //ClassB *newClassB = [[[self class] allocWithZone: zone] init];
  65. //newClassB.age = self.age;
  66. // 考虑到父类可能已经实现了数据复制
  67. ClassB *newClassB = (ClassB *)[super copyWithZone: zone];
  68. // TODO:: 其他设值
  69. return newClassB;
  70. }
  71. @end
  72.  
  73. int main(int argc, const char * argv[]) {
  74. @autoreleasepool {
  75. ClassA *classA = [[ClassA alloc] init];
  76. classA.age = 13;
  77. classA.theName = @"nameA";
  78. ClassA *copyClassA = [classA copy];
  79. NSLog(@"copy class A: age=%i, theName=%@", copyClassA.age, copyClassA.theName);
  80. copyClassA.age = 14;
  81. ClassA *mcopyClassA = [classA mutableCopy];
  82. NSLog(@"mutable copy class A: age=%i, theName=%@", mcopyClassA.age, mcopyClassA.theName);
  83. ClassB *classB = [[ClassB alloc] init];
  84. classB.age = 15;
  85. classB.theName = @"nameB";
  86. ClassB *copyClassB = [classB copy];
  87. NSLog(@"copy class B: age=%i, theName=%@", copyClassB.age, copyClassB.theName);
  88. ClassB *mcopyClassB = [classB mutableCopy];
  89. NSLog(@"mutable class B: age=%i, theName=%@", mcopyClassB.age, mcopyClassB.theName);
  90. mcopyClassB.theName = @"changeNameB";
  91. NSLog(@"classB.theName=%@", classB.theName);
  92. }
  93. return 0;
  94. }

运行测试
111.png

标签: Objective-C

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号