ChenYilong / ChenYilong/iOSInterviewQuestions

第13题 疑问 对非集合类对象的copy操作

Open
#19 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
Swift
Stars
9.5k
Forks
2.8k
PR merge metrics
No merged PRs in 30d

Description

题目中给出的链接:https://www.zybuluo.com/MicroCai/note/50592

```
NSMutableString *string = [NSMutableString stringWithString: @"origin"];
//copy
NSString *stringCopy = [string copy];
NSMutableString *mStringCopy = [string copy];
NSMutableString *stringMCopy = [string mutableCopy];
//change value
[mStringCopy appendString:@"mm"]; //crash
[string appendString:@" origion!"];
[stringMCopy appendString:@"!!"];
```

问什么会在这么一行Crash

```
[mStringCopy appendString:@"mm"]; //crash
```

根据他得描述,『原因就是 copy 返回的对象是 immutable 对象』, 难道不应该是下面这一行出错吗?

```
NSMutableString *mStringCopy = [string copy];
```

copy 返回的是immutable,而所要复制的对象是 mutable.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.