文件标签:string,NSLog,age,OC,str,拷贝,copy,name From: https://blog.51cto.com/u_15907570/5925312
#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