当前位置:网站首页>Objective - C code analysis of the deep and shallow copy
Objective - C code analysis of the deep and shallow copy
2022-08-03 11:10:00 【Atemwood】
ocThis language belongs to the oddest of odds,Objects of basic types are divided into“可变”类型与“不可变”类型.
Such as the following types
可变 | 不可变 |
---|---|
NSArray | NSMutableArray |
NSString | NSMutableString |
NSNumber | NSMutableNumber |
… | … |
Mutable types can be seen as“变量”,Immutable types can be thought of as“常量”.当然,Only superficially similar.
两种类型都是NSObject
的子类,都实现了NSObject
中的mutableCopy
与copy
方法.
about these two methods,Apple是这样介绍的:
在oc中 copy和mutableCopyTwo methods are used by all objects(继承自NSObject的类)继承的,These two methods are forcopy准备的.其中,mutableCopyis to create mutable types of primitive objectscopy.这两个方法分别调用copyWithZone和mutableCopyWithZoneTwo ways to do itcopy.A class must implementcopyWithZone或者mutableCopyWithZone,才能进行copy或者mutableCopy.
两种类型(可变、不可变),两种方法(mutableCopy、copy),There are four combinations:
- 可变类型调用copy
- 可变类型调用mutableCopy
- 不可变类型调用copy
- 不可变类型调用mutableCopy
These four combinations derive two concepts,浅拷贝
与深拷贝
:
- 浅copy: 指针复制,A new object will not be created.
- 深copy: 内容复制,会创建一个新的对象.
concept space,用代码来理解:
#import <Foundation/Foundation.h>
int main(int args, const char *argv[]) {
@autoreleasepool {
// 可变对象调用copy,mutableCopy
NSMutableString *mutableString1 = [[NSMutableString alloc] init];
[mutableString1 setString:@"hello1"];
id s1 = mutableString1.copy;
id s2 = mutableString1.mutableCopy;
NSLog(@"可变对象:%p %@", mutableString1,mutableString1.class);
NSLog(@"调用copy:%p %@", s1, [s1 class]);
NSLog(@"调用mutableCopy:%p %@ \n\n", s2, [s2 class]);
// 可变对象调用copy,mutableCopy
NSString *immutableString1 = @"hello2";
id s3 = immutableString1.copy;
id s4 = immutableString1.mutableCopy;
NSLog(@"不可变对象:%p %@", immutableString1,immutableString1.class);
NSLog(@"调用copy:%p %@", s3, [s3 class]);
NSLog(@"调用mutableCopy:%p %@", s4, [s4 class]);
}
return 0;
}
以第一部分“可变对象调用copy,mutableCopy”为例
Create a mutable object first,Then set a value for it“hello1”
NSMutableString *mutableString1 = [[NSMutableString alloc] init];
[mutableString1 setString:@"hello1"];
因为我们不知道copy
与mutableCopy
What type of object is returned,So we use twoid
变量来获取
id s1 = mutableString1.copy;
id s2 = mutableString1.mutableCopy;
%p
The value of the pointer can be printed out,[s1 class]
Can know which class it is,So the original variable can be printed out、copy对象和mutableCopyThe memory address and type of the object,如下.
NSLog(@"可变对象:%p %@", mutableString1,mutableString1.class);
NSLog(@"调用copy:%p %@", s1, [s1 class]);
NSLog(@"调用mutableCopy:%p %@ \n\n", s2, [s2 class]);
打印结果如下(Omit irrelevant parts):
可变对象:0x600003058ba0 __NSCFString
调用copy:0xaf10ee324c733912 NSTaggedPointerString
调用mutableCopy:0x600003058bd0 __NSCFString
可以看出,All three memory addresses are different,Indicates that a new object was created,因此
Mutable type callscopy与mutableCopy都是深拷贝
Take another look at the code for the immutable part
NSString *immutableString1 = @"hello2";
id s3 = immutableString1.copy;
id s4 = immutableString1.mutableCopy;
NSLog(@"不可变对象:%p %@", immutableString1,immutableString1.class);
NSLog(@"调用copy:%p %@", s3, [s3 class]);
NSLog(@"调用mutableCopy:%p %@", s4, [s4 class]);
Basically similar to the previous code,直接看输出:
不可变对象:0x100ce40e8 __NSCFConstantString
调用copy:0x100ce40e8 __NSCFConstantString
调用mutableCopy:0x600001980cc0 __NSCFString
可以看出:
- 不可变对象调用copy:浅拷贝
- 不可变对象调用mutableCopy:深拷贝
一句话总结:
Only immutable objects are calledcopy是浅拷贝,其他都是深拷贝
有关copy的深度长文:https://www.jianshu.com/p/5f776a4816ee
边栏推荐
- numpy
- Depth study of 100 cases - convolution neural network (CNN) to realize the clothing image classification
- What is the relationship between The Matrix and 6G?
- 【输出一个整数的的每一位,由高到低输出。使用递归和不使用递归】
- 直播弱网优化
- Who is more popular for hybrid products, depending on technology or market?
- ScrollView嵌套RecyclerView滚动冲突
- build --repot
- Basic using MySQL database
- 【冒泡排序以及奇数偶数排列】
猜你喜欢
SAP 电商云 Spartacus UI 的 External Routes 设计明细
Machine Learning Overview
「全球数字经济大会」登陆 N 世界,融云提供通信云服务支持
Cross-chain bridge protocol Nomad suffers hacker attack, losing more than $150 million
【冒泡排序以及奇数偶数排列】
Depth study of 100 cases - convolution neural network (CNN) to realize the clothing image classification
ETL data cleaning case in MapReduce
XDR平台架构与关键技术解析
【二分查找详解外加递归写法】附有全部代码
numpy
随机推荐
[错题]电路维修
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之二
Depth study of 100 cases - convolution neural network (CNN) to realize the clothing image classification
机器学习概述
Skills required to be a good architect: How to draw a system architecture that everyone will love?What's the secret?Come and open this article to see it!...
完全背包问题的思路解析
本周四晚19:00知识赋能第4期直播丨OpenHarmony智能家居项目之设备控制实现
【TypeScript】Why choose TypeScript?
在安装GBase 8c数据库的时候,报错显示“Host ips belong to different cluster”。这是为什么呢?有什么解决办法?
[LeetCode—Question 2 Sum of Two Numbers Detailed Code Explanation ] The source code is attached, which can be copied directly
Classical Architecture and Memory Classification of Embedded Software Components
试题G:单词分析 ← 第十一届蓝桥杯大赛第二场省赛赛题
深度学习100例——卷积神经网络(CNN)实现服装图像分类
【冒泡排序以及奇数偶数排列】
Analysis of the idea of the complete knapsack problem
synchronized
关于OPENSSL的问题
如何改变sys_guid() 返回值类型
3分钟实现内网穿透(基于ngrok实现)
用于发票处理的 DocuWare,摆脱纸张和数据输入的束缚,自动处理所有收到的发票