copy和mutableCopy方法

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

示例一:数组元素是不可变字符串

  1. #import <Foundation/Foundation.h>
  2.  
  3. int main(int argc, const char * argv[]) {
  4. @autoreleasepool {
  5. NSMutableArray *dataArray = [NSMutableArray arrayWithObjects:
  6. @"one", @"two", @"three", @"four", nil];
  7. NSMutableArray *dataArray2;
  8. // 简单赋值
  9. dataArray2 = dataArray;
  10. [dataArray2 removeObjectAtIndex: 0];
  11. NSLog(@"dataArray:");
  12. for (NSString *elem in dataArray)
  13. NSLog(@" %@", elem);
  14. NSLog(@"dataArray2:");
  15. for (NSString *elem in dataArray2)
  16. NSLog(@" %@", elem);
  17. // 复制一份,然后删除副本的第一个元素
  18. // 产生一个对象的可变字符串并不要求被复制的对象本身是可变的。
  19. // 这种情况同样适用于不可变副本:可以创建可变对象的不可变副本。
  20. dataArray2 = [dataArray mutableCopy];
  21. [dataArray2 removeObjectAtIndex: 0];
  22. NSLog(@"dataArray: ");
  23. for (NSString *elem in dataArray)
  24. NSLog(@" %@", elem);
  25. NSLog(@"dataArray2:");
  26. for (NSString *elem in dataArray2)
  27. NSLog(@" %@", elem);
  28. }
  29. return 0;
  30. }

运行测试
111.png

示例二:数组元素是可变字符串

  1. #import <Foundation/Foundation.h>
  2.  
  3. int main(int argc, const char * argv[]) {
  4. @autoreleasepool {
  5. NSMutableArray *dataArray = [NSMutableArray arrayWithObjects:
  6. [NSMutableString stringWithString: @"one"],
  7. [NSMutableString stringWithString: @"two"],
  8. [NSMutableString stringWithString: @"three"],
  9. nil];
  10. NSMutableArray *dataArray2;
  11. NSMutableString *mStr;
  12. NSLog(@"dataArray:");
  13. for (NSString *elem in dataArray)
  14. NSLog(@" %@", elem);
  15. // 复制一份,然后删除副本的第一个元素
  16. // mutableCopy仅复制了数组中可变字符串对象的引用
  17. dataArray2 = [dataArray mutableCopy];
  18. mStr = dataArray[0];
  19. [mStr appendString: @"ONE"];
  20. NSLog(@"dataArray: ");
  21. for (NSString *elem in dataArray)
  22. NSLog(@" %@", elem);
  23. NSLog(@"dataArray2:");
  24. for (NSString *elem in dataArray2)
  25. NSLog(@" %@", elem);
  26. }
  27. return 0;
  28. }

运行测试
111.png

示例三:重写示例二

  1. #import <Foundation/Foundation.h>
  2.  
  3. int main(int argc, const char * argv[]) {
  4. @autoreleasepool {
  5. NSMutableArray *dataArray = [NSMutableArray arrayWithObjects:
  6. [NSMutableString stringWithString: @"one"],
  7. [NSMutableString stringWithString: @"two"],
  8. [NSMutableString stringWithString: @"three"],
  9. nil];
  10. NSMutableArray *dataArray2;
  11. NSMutableString *mStr;
  12. NSLog(@"dataArray:");
  13. for (NSString *elem in dataArray)
  14. NSLog(@" %@", elem);
  15. // 复制一份,然后删除副本的第一个元素
  16. // mutableCopy仅复制了数组中可变字符串对象的引用
  17. dataArray2 = [dataArray mutableCopy];
  18. //mStr = dataArray[0];
  19. //[mStr appendString: @"ONE"];
  20. // 对想改变的元素创建一个副本
  21. mStr = [NSMutableString stringWithString: dataArray2[0]];
  22. [mStr appendString: @"ONE"];
  23. [dataArray2 replaceObjectAtIndex: 0 withObject: mStr];
  24. NSLog(@"dataArray: ");
  25. for (NSString *elem in dataArray)
  26. NSLog(@" %@", elem);
  27. NSLog(@"dataArray2:");
  28. for (NSString *elem in dataArray2)
  29. NSLog(@" %@", elem);
  30. }
  31. return 0;
  32. }

运行测试
111.png

标签: Objective-C

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号