文件
#import <Foundation/Foundation.h>
#import "Student.h"
#import "GoodStudent.h"
// copy语法的目的:改变副本的时候,不会影响到源对象
字符串的拷贝(深拷贝)
// 深拷贝:内容拷贝,会产生新的对象。新对象计数器置为1,源对象计数器不变。
void
// string:1
NSString *string = [[NSString alloc] initWithFormat:@"age is %i", 10];
// 产生了一个新的对象,计数器为1。源对象的计数器不变。
// str:1
// string:1
NSMutableString *str = [string mutableCopy];
//NSLog(@"str:%zi", [str retainCount]);
//NSLog(@"string:%zi", [string retainCount]);
// str和string不是相同对象
// NSLog(@"%i", str == string);
appendString:@" abcd"];
NSLog(@"string:%@", string);
NSLog(@"str:%@", str);
// str:0
release];
// string:0
release];
}
演示字符串的拷贝(浅拷贝)
// 只有一种情况是浅拷贝:不可变对象调用copy方法时
// 浅拷贝:指针拷贝,不会产生新的对象。源对象计数器+1。
void
NSString *string = [[NSString alloc] initWithFormat:@"age is %i", 10];
NSLog(@"%zi", [string retainCount]);
// copy产生的是不可变副本,由于源对象本身就不可变,所以为了性能着想,copy会直接返回源对象本身
// 源对象计数器会+1
// 在浅拷贝情况下,copy其实就相当于retain
NSString *str = [string copy];
NSLog(@"%zi", [string retainCount]);
// NSLog(@"%i", str == string);
release];
release];
}
可变字符串的copy(深拷贝)
void
NSMutableString *string = [NSMutableString stringWithFormat:@"age is %i", 10];
// 会产生一个新对象,str计数器为1
NSString *str = [string copy];
release];
}
可变字符串的MutableCopy(深拷贝)
void
NSMutableString *string = [NSMutableString stringWithFormat:@"age is %i", 10];
// 会产生一个新对象,str计数器为1
NSMutableString *str = [string mutableCopy];
appendString:@"1234"];
NSLog(@"str:%@", str);
NSLog(@"string:%@", string);
release];
}
的name的copy
void
Student *stu = [[[Student alloc] init] autorelease];
NSMutableString *string = [NSMutableString stringWithFormat:@"age is %i", 10];
name
appendString:@"abcd"];
NSLog(@"name=%@", stu.name);
NSLog(@"string=%@", string);
}
的copy
void
Student *stu1 = [Student studentWithName:@"stu1"];
Student *stu2 = [stu1 copy];
name = @"stu2";
NSLog(@"stu1:%@", stu1);
NSLog(@"stu2:%@", stu2);
release];
}
void
GoodStudent *stu1 = [GoodStudent goodStudentWithAge:10 name:@"good1"];
GoodStudent *stu2 = [stu1 copy];
name = @"good2";
age = 11;
NSLog(@"stu1:%@", stu1);
NSLog(@"stu2:%@", stu2);
}
int main(int argc, const char
{
@autoreleasepool {
goodStudentCopy();
}
return 0;
}
文件
#import <Foundation/Foundation.h>
@interface Student : NSObject <NSCopying>
代表set方法会release旧对象、copy新对象
// 修改外面的变量,并不会影响到内部的成员变量
建议:NSString一般用copy策略,其他对象一般用retain
(nonatomic, copy) NSString *name;
+ (id)studentWithName:(NSString
@end
文件
#import "Student.h"
Student
+ (id)studentWithName:(NSString
// 这里最好写[self class]
Student *stu = [[[[self class] alloc] init] autorelease];
name
return
}
- (void)dealloc {
_name release];
super dealloc];
}
方法内部不能打印self,不然会造成死循环
- (NSString
return [NSString stringWithFormat:@"[name=%@]", _name];
}
协议的方法
// 这里创建的副本对象不要求释放
- (id)copyWithZone:(NSZone
Student *copy = [[[self class] allocWithZone:zone] init];
// 拷贝名字给副本对象
name = self.name;
return
}
@end
GoodStudent.h
#import "Student.h"
@interface GoodStudent : Student
(nonatomic, assign) int age;
+ (id)goodStudentWithAge:(int)age name:(NSString
@end
GoodStudent.m
#import "GoodStudent.h"
GoodStudent
+ (id)goodStudentWithAge:(int)age name:(NSString
GoodStudent *good = [GoodStudent studentWithName:name];
age
return
}
- (NSString
return [NSString stringWithFormat:@"[name=%@, age=%i]", self.name, _age];
}
- (id)copyWithZone:(NSZone
// 一定要调用父类的方法
GoodStudent *copy = [super copyWithZone:zone];
age = self.age;
return
}
@end
OC之【深拷贝(mutableCopy)和浅拷贝(copy)】
原创wx6392bc05d177b 博主文章分类:【IOS移动端】 ©著作权
©著作权归作者所有:来自51CTO博客作者wx6392bc05d177b的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:OC之【@class】
下一篇:OC之【NSNumber的使用】
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Python copy模块浅拷贝和深拷贝
Python copy模块浅拷贝和深拷贝在开发中,经常涉及到数据的传递,在数据传递使用的过程
Python copy模块浅拷贝和深拷贝 Python 实现数据拷贝的方法 Python 中的引用和引用传递 浅拷贝时数据被修改 深拷贝保证数据不会被修改 -
57 - 浅拷贝(copy)和深拷贝(deepcopy)
深拷贝和浅拷贝的区别
深拷贝 浅拷贝 拷贝 python 面试题 -
javascript深拷贝和浅拷贝 js 深拷贝浅拷贝
目录一、区别浅拷贝(shallow copy):深拷贝(deep copy)二、实现方法:浅拷贝1、直接赋值法深拷贝1、Object.assign()2、转成JSON3、递归4. 通过jQuery的extend方法实现深拷贝5. lodash函数库实现深拷贝一、区别浅拷贝(shallow copy): 复
javascript深拷贝和浅拷贝 javascript 开发语言 ecmascript 前端