首页 > 其他分享 >OC之【深拷贝(mutableCopy)和浅拷贝(copy)】

OC之【深拷贝(mutableCopy)和浅拷贝(copy)】

时间:2022-12-09 15:04:07浏览次数:38  
标签:string NSLog age OC str 拷贝 copy name

文件

#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

标签:string,NSLog,age,OC,str,拷贝,copy,name
From: https://blog.51cto.com/u_15907570/5925312

相关文章

  • OC之【NSNumber的使用】
    #import<Foundation/Foundation.h>void//将int类型的10包装成一个NSNumber对象NSNumber*number=[NSNumbernumberWithInt:10];NSLog(@"number=%@",number);......
  • OC之【NSDictionary详解】
    main.m文件#import<Foundation/Foundation.h>#import"Student.h"字典的初始化void//NSDictionary是不可变的NSDictionary*dict=[NSDictionarydictionaryWit......
  • OC之【c语言结构体】
    <stdio.h>void//这个机构只能在函数内部使用//定义一个名为Student的结构体类型structintage;//年龄char*name;//姓名floatheight;//身高};......
  • OC之【NSMutableDictionary的使用】
    main.m文件#import<Foundation/Foundation.h>#import"Student.h"可变字典的使用void//创建一个空的字典NSMutableDictionary*dict=[NSMutableDictionarydicti......
  • OC之【NSValue的使用】
    #import<Foundation/Foundation.h>voidCGPointpoint=CGPointMake(10,10);//将结构体变量包装成一个对象NSValue*value=[NSValuevalueWithPoint:point];......
  • OC之【enum枚举】
    void//定义一种枚举类型enum//定义一个枚举变量senumSeasons=winter;}void//定义枚举类型的同时定义一个枚举变量senumSeason{spring,summer,......
  • OC之【NSDate使用】
    #import<Foundation/Foundation.h>日期创建void//date方法返回的就是当前时间(now)NSDate*date=[NSDatedate];//now:21:09:40//date:21:09:50......
  • OC之【内存管理】
    Student@synthesizeage=_age;//在xcode4.5以上环境下可以省略-(void)dealloc{@"%@被销毁了",self);super//一定要调用super的dealloc方法,而且最好放在最......
  • OC之【NSObject使用】
    main.m文件#import<Foundation/Foundation.h>#import"Student.h"#import"Person.h"常用方法voidStudent*stu=[[[Studentalloc]init]autorelease];//isKin......
  • OC之【@property的用法】
    1.这里的retain代表:在set方法中,release旧值,retain新值(nonatomic,retain)Book*book;(retain)Card*card;代表只生成get方法的声明默认是readwrite,同时生成get和set......