xml地图|网站地图|网站标签 [设为首页] [加入收藏]

皇家赌场深浅拷贝与strong,浅拷贝与copy

来源:http://www.ccidsi.com 作者:最新解决方案 人气:57 发布时间:2020-03-21
摘要:property里的copy、strong区别 说完深浅拷贝,通晓property里的copy、strong就轻便多了! copy #import Foundation/Foundation.h@interface HSPerson : NSObject@property (nonatomic, copy) NSString *name;@endNSMutableString *str

property里的copy、strong区别

说完深浅拷贝,通晓property里的copy、strong就轻便多了!

  • copy
#import <Foundation/Foundation.h>@interface HSPerson : NSObject@property (nonatomic, copy) NSString *name;@end

NSMutableString *string = [NSMutableString stringWithFormat:@"汉斯哈哈哈"];HSPerson *person = [[HSPerson alloc] init];person.name = string;// 不能改变person.name的值,因为其内部copy新的对象[string appendString:@" hans"]; NSLog(@"name = %@", person.name);

皇家赌场 1

property copy 实际上就对name干了那一个:

- setName:(NSString *)name{ _name = [name copy];}

假设name为NSMutableString,会发生什么事?

@property (nonatomic, copy) NSMutableString *name;

这么会挨骂哦,实际上里面依然:

- setName:(NSMutableString *)name{ _name = [name copy];}

copy出来的照样是不足变字符!假如有人用NSMutableString的艺术,就能崩溃:

皇家赌场 2

  • strong
@property (nonatomic, strong) NSString *name;

NSMutableString *string = [NSMutableString stringWithFormat:@"汉斯哈哈哈"];HSPerson *person = [[HSPerson alloc] init];person.name = string;// 可以改变person.name的值,因为其内部没有生成新的对象[string appendString:@" hans"];NSLog(@"name = %@", person.name);

皇家赌场 3

随笔同步到Wechat公众号:hans_iOS 有疑点能够在万众号里直接发

property里的copy、strong区别

讲罢深浅拷贝,精通copy.strong就自在多了!

copy

#import <Foundation/Foundation.h>
@interface HSPerson : NSObject
@property (nonatomic, copy) NSString *name;
@end
NSMutableString *string = [NSMutableString stringWithFormat:@"汉斯哈哈哈"];
HSPerson *person = [[HSPerson alloc] init];
person.name = string;

// 不能改变person.name的值,因为其内部copy新的对象
[string appendString:@" hans"];

 NSLog(@"name = %@", person.name);
property copy 实际上就对name干了这个:

- (void)setName:(NSString *)name
{
    _name = [name copy];
}

假定name为NSMutableString,会发生怎样事?

@property (nonatomic, copy) NSMutableString *name;

如此会挨骂哦,实际上里面照旧:

- (void)setName:(NSMutableString *)name
{
    _name = [name copy];
}

copy出来的仍然为不可变字符!假使有人用NSMutableString的法门,就能崩溃:

strong

@property (nonatomic, strong) NSString *name;
NSMutableString *string = [NSMutableString stringWithFormat:@"汉斯哈哈哈"];

HSPerson *person = [[HSPerson alloc] init];
person.name = string;

// 可以改变person.name的值,因为其内部没有生成新的对象
[string appendString:@" hans"];

NSLog(@"name = %@", person.name);

计算:用copy与strong决计于需要,要是不期望被外部改正用copy,反之用strong

皇家赌场 4

深.浅拷贝

  • copy/mutableCopy NSString
NSString *string = @"汉斯哈哈哈";// 没有产生新对象NSString *copyString = [string copy];// 产生新对象NSMutableString *mutableCopyString = [string mutableCopy];NSLog(@"string = %p copyString = %p mutableCopyString = %p", string, copyString, mutableCopyString);

皇家赌场 5

  • copy/mutableCopy NSMutableString
NSMutableString *string = [NSMutableString stringWithString:@"汉斯哈哈哈"];// 产生新对象NSString *copyString = [string copy];// 产生新对象NSMutableString *mutableCopyString = [string mutableCopy];NSLog(@"string = %p copyString = %p mutableCopyString = %p", string, copyString, mutableCopyString);

皇家赌场 6

结论:

皇家赌场 7

在乎:其余对象NSArray、NSMutableArray 、NSDictionary、NSMutableDictionary相符适用

皇家赌场 8

  • copy NSObject
HSPerson *p = [[HSPerson alloc] init];p.age = 20;p.height = 170.0;HSPerson *copyP = [p copy]; // 这里崩溃

崩溃:

皇家赌场 9

看崩溃音信HSPerson应该先完结:

- copyWithZone:zone;

测试:

#import "HSPerson.h"@interface HSPerson()<NSCopying>@end@implementation HSPerson- copyWithZone:zone{ return @"汉斯哈哈哈";}@end

HSPerson *p = [[HSPerson alloc] init];p.age = 20;p.height = 170.0;HSPerson *copyP = [p copy];NSLog(@"copyP: %@", copyP);

皇家赌场 10

能够看看copyWithZone重新分配新的内部存款和储蓄器空间,则:

- copyWithZone:zone{ HSPerson *person = [[HSPerson allocWithZone:zone] init]; return person;// 有些人可能下面alloc,重新初始化空间,但这方法已给你分配了zone,自己就无需再次alloc内存空间了// HSPerson *person = [[HSPerson alloc] init];}

HSPerson *p = [[HSPerson alloc] init];p.age = 20;p.height = 170.0;HSPerson *copyP = [p copy];NSLog(@"p = %p copyP = %p", p, copyP);NSLog(@"age = %d height = %f", copyP.age, copyP.height);

皇家赌场 11

即使copy了份新的对象,不过age,height值并未有copy,那么:

- copyWithZone:zone{ HSPerson *person = [[HSPerson allocWithZone:zone] init]; person.age = self.age; person.height = self.height; // 这里self其实就要被copy的那个对象,很显然要自己赋值给新对象,所以这里可以控制copy的属性 return person;}

皇家赌场 12

此刻你会想,有NSMutableCopying?没有错,是有这货:

- mutableCopyWithZone:zone{ HSPerson *person = [[HSPerson allocWithZone:zone] init]; person.age = self.age; person.height = self.height; return person;}

NSCopying、NSMutableCopying有甚差距?其实感到没必要有NSMutableCopying,因为压根就没可变的HSPerson,但假使该对象有别的表现,能够借用NSMutableCopying达成,哈哈哈

深层复制:拷贝容器自己,再次来到八个目的,指向不一致的内部存款和储蓄器地址.

举例NSArray、NSDictionary,它们曾经落到实处了地点多少个研商。
对于它们来说,法规不会细小略,obj2 = [obj1 copy]再次来到的终将是二个不可变对象,不论obj1是可变对象照旧不可变对象。如果obj1是多个不可变对象,那么它们照准同贰个目的,也是上一条自己关系过的。
obj2 = [obj1 mutableCopy]回去的任其自流是几个可变对象,无论obj1是可变对象依旧不可变对象。纵然obj1也是二个可变对象,它们仍指向区别地点,是三个目的。

留心:其余对象NSArray、NSMutableArray 、NSDictionary、NSMutableDictionary近似适用

HSPerson *p = [[HSPerson alloc] init];
p.age = 20;
p.height = 170.0;
HSPerson *copyP = [p copy]; # 这里崩溃

看崩溃音讯HSPerson应该先完结:

- (id)copyWithZone:(NSZone *)zone;

测试:

#import "HSPerson.h"
@interface HSPerson()<NSCopying>
@end

@implementation HSPerson

- (id)copyWithZone:(NSZone *)zone
{

    return @"汉斯哈哈哈";
}
@end

HSPerson *p = [[HSPerson alloc] init];
p.age = 20;
p.height = 170.0;

HSPerson *copyP = [p copy];
NSLog(@"copyP: %@", copyP);

能够看看copyWithZone重新分配新的内部存款和储蓄器空间,则:

- (id)copyWithZone:(NSZone *)zone
{
    HSPerson *person = [[HSPerson allocWithZone:zone] init];
    return person;

// 有些人可能下面alloc,重新初始化空间,但这方法已给你分配了zone,自己就无需再次alloc内存空间了
//    HSPerson *person = [[HSPerson alloc] init];
}
HSPerson *p = [[HSPerson alloc] init];
p.age = 20;
p.height = 170.0;

HSPerson *copyP = [p copy];
NSLog(@"p = %p copyP = %p", p, copyP);

NSLog(@"age = %d height = %f", copyP.age, copyP.height);

虽说copy了份新的靶子,可是age,height值并未有copy,那么:

- (id)copyWithZone:(NSZone *)zone
{
    HSPerson *person = [[HSPerson allocWithZone:zone] init];
    person.age = self.age;
    person.height = self.height;
    // 这里self其实就要被copy的那个对象,很显然要自己赋值给新对象,所以这里可以控制copy的属性
    return person;
}

那会儿你会想,有NSMutableCopying?对的,是有那货:

- (id)mutableCopyWithZone:(NSZone *)zone
{
    HSPerson *person = [[HSPerson allocWithZone:zone] init];
    person.age = self.age;
    person.height = self.height;

    return person;
}

NSCopying、NSMutableCopying有甚分裂?
事实上呼吸系统感染觉没供给有NSMutableCopying,因为压根就没可变的HSPerson,但一旦该目的有别的表现,能够借用NSMutableCopying落成,哈哈哈

property copy 实际上就对name干了那些:

浅拷贝:拷贝轻巧自身,重回贰个指标,指向近似的内部存款和储蓄器地址.

- (id)copyWithZone:(NSZone*)zone{    HSPerson *person = [[HSPerson allocWithZone:zone] init];    person.age =self.age;    person.height =self.height;// 这里self其实就要被copy的可怜目标,很明显要团结赋值给新指标,所以那边可以调整copy的性质returnperson;}

copy

@property(nonatomic,strong)NSString*name;

- (void)setName:(NSMutableString*)name{    _name = [namecopy];}

copy NSObject

皇家赌场 13

皇家赌场 14

皇家赌场 15

皇家赌场 16

property里的copy、strong区别

- (void)setName:(NSString*)name{    _name = [namecopy];}

看崩溃音讯HSPerson应该先达成:

@property(nonatomic,copy)NSMutableString*name;

能够见见copyWithZone重新分配新的内部存款和储蓄器空间,则:

此时你会想,有NSMutableCopying?没有错,是有那货:

本文由68399皇家赌场发布于最新解决方案,转载请注明出处:皇家赌场深浅拷贝与strong,浅拷贝与copy

关键词: 68399皇家赌场 日记本 iOS 浅谈 strong

最火资讯