主要 原则是:
Copy 可变对象 A 生成 一个 不可变的 B
Copy 不可变对象 C 不会生成新的对象 只是浅拷贝 拷贝指针
MutableCopy 不论是 可变还是 不可变的 对象 都生成 一个可变的 新的 对象 。
#pragma mark ---- NSString 的copy实例
/**
我们发现对一个NSString 的copy后指针并没有发生改变
而对一个 NSString 的 mutableCopy后指针发生了改变
*/
- (void)stringCopyAndMutableCopyOne
{
NSString *str = [NSStringstringWithFormat:@"age is %i", 10];
NSString *str1 = [strcopy];
NSLog(@"%i", str == str1); // 1
NSMutableString *str2 = [strmutableCopy];
NSLog(@"%i", str2 == str); // 0
}
- (void)copyCompare
{
NSMutableString *str=[NSMutableStringstringWithFormat:@"age is %i", 10];
id str1=[strcopy];
if ([str1isKindOfClass:[NSMutableStringclass]]) {
NSLog(@"YES");
}else{
NSLog(@"NO");
}
}
/**
对NSMutableString执行 copy 和 nsmutableCopy
str 是 mutable
*/
- (void)nsmutableStringCopyAndMutableCopy
{
NSMutableString *str=[NSMutableStringstringWithFormat:@"age is %i",11];
NSString *str1=[strmutableCopy];
//
NSMutableString *str2=[strmutableCopy];
NSLog(@"%i", str == str1); // 0
NSLog(@"%i", str2 == str); // 0
}