gpt4 book ai didi

objective-c - 属性中的(copy)属性是否有实际意义?

转载 作者:搜寻专家 更新时间:2023-10-30 20:00:07 24 4
gpt4 key购买 nike

我假设copy属性的目的是为了将一个属性分配给另一个属性(比如说),并让底层的getter/setter方法正确地处理所有事情。但是,在回顾了一些文章(包括一些关于StAcExpLoad)的情况下,我不清楚,仅仅使用适当的方法来显式地复制内容,有什么真正的好处。首先,我发现将可变字符串分配给另一个可变字符串仍然会留下一个不可变字符串,那有什么好处呢?
这对其他课程有效吗?换句话说,一般来说,如果我有

 @property (copy) Foo* f1;
@property (copy) Foo* f2;
....
@property (copy) Foo* fn;

我一般能写吗?
f1 = f2
f3 = f2

得到正确的副本,即完全独立的对象?

最佳答案

(copy)将对正在设置的项调用copy方法,因此它将取决于对象。让我们看一个使用nsstring的示例,看看这是否有帮助?
两个类ObjectWithCopy

@interface ObjectWithCopy : NSObject

@property (copy) NSString *aString;

@end

以及 ObjectWithoutCopy
@interface ObjectWithoutCopy : NSObject

@property (strong) NSString *aString;

@end

到目前为止相当简单,而且我们都知道nsstring是不可变的,那么“copy”将做什么呢?请记住,nsmutablestring是nsstring的一个子类。所以我们可以把它传给二传手。我们这样做会怎么样?
NSMutableString *aMutableString = [[NSMutableString alloc] initWithString:@"Hello World"];

ObjectWithCopy *objectWithCopy = [[ObjectWithCopy alloc] init];
objectWithCopy.aString = aMutableString;
NSLog(@"ObjectWithCopy.aString = %@", objectWithCopy.aString);

//Now we change aMutableString
[aMutableString appendString:@" and every other world"];
NSLog(@"ObjectWithCopy.aString after aMutableString was modified = %@", objectWithCopy.aString);


NSLog(@"Now what happens without copy?");

// Reset aMutableString
aMutableString = [[NSMutableString alloc] initWithString:@"Hello World"];

ObjectWithoutCopy *objectWithoutCopy = [[ObjectWithoutCopy alloc] init];
objectWithoutCopy.aString = aMutableString;
NSLog(@"ObjectWithoutCopy.aString = %@", objectWithoutCopy.aString);

//Now we change aMutableString and see what objectWithoutCopy.aString is?
[aMutableString appendString:@" and every other world"];
NSLog(@"ObjectWithoutCopy.aString after aMutableString was modified = %@", objectWithoutCopy.aString);

输出?
2014-02-02 10:40:04.247 TableViewCellWithAutoLayout[95954:a0b] ObjectWithCopy.aString = Hello World
2014-02-02 10:40:04.248 TableViewCellWithAutoLayout[95954:a0b] ObjectWithCopy.aString after aMutableString was modified = Hello World
2014-02-02 10:40:04.248 TableViewCellWithAutoLayout[95954:a0b] Now what happens without copy?
2014-02-02 10:40:04.248 TableViewCellWithAutoLayout[95954:a0b] ObjectWithoutCopy.aString = Hello World
2014-02-02 10:40:04.249 TableViewCellWithAutoLayout[95954:a0b] ObjectWithoutCopy.aString after aMutableString was modified = Hello World and every other world

哇-我们的不可变字符串在 ObjectWithoutCopy上被更改了!?!?那是因为它毕竟是一个nsmutablestring,没有拷贝,我们只是指向传入的内容。因此,传入对象发生的任何更改都将在类和函数变量中看到。这就是为什么经常有人建议,当您有一个使用 (copy)的nsstring属性时,您不会期望nsstring发生更改。
copy使assign语句成为 _aString = [passedInString copy];。正如一些人指出的,只要它符合 NSCopying protocol,就可以做任何事情,但是应该创建对象的独立副本,以确保外部世界所做的更改不会影响您的类。因此,有人可以创建nsstring( which you probably should never do)的子类,该子类重写copy而不执行任何操作,并且您仍然可以看到nsstring从您的下面改变,但通常情况下,nscoping是正确完成的,这是非常安全的。至少你能做到最好。
编辑1:
正如@david在评论中提到的(也许这是他的真正问题?), [aMutableString copy]实际上返回 NSString类型的对象。我完全同意这是令人困惑的。如果我在做 NSMutableString的话,我会让copy return a NSMutableString。实际上,我认为真正的wtf是苹果创建了一个不可变类的可变子类。在c/java中,没有可变字符串,而是使用另一个名为stringbuilder的类。
所以,如果这是如此令人困惑,为什么这么多人使用 copyon NSString属性?因为它很可能做了你真正想做的事。如果您的类有一个 NSString属性,那么您选择这个属性是因为您不想担心字符串从您的下面改变。为此,您确实需要传入字符串的副本。如果它是一个 NSMutableString你可能想要一个 NSString副本,因为你不想给这个可变的字符串,并把它从你下面改出来。如果copy on a NSMutableString返回了一个 NSMutableString,我认为人们不会使用copy属性,而是创建一个设置 _aString = [[NSString alloc] initWithString: inputString]的自定义setter。这可能更清楚,如果你认为这可能是你应该做的。但此时使用copy on NSString属性是惯例,所以我可能会继续使用它。你可能同意,也可能不同意,但你的问题是为什么人们使用副本,这就是为什么我个人这么做。
编辑2:
@大卫问为什么复制不这样做?”如果我复制一个字符串,那么我希望随后的调用能够更改它。“我认为如果您真的想要副作用,那么可能应该声明一个不带copy修饰符的nsmutablestring( you probably should never use copy and NSMutableString,但是创建自己的setter)。
在我近15年的编程生涯中,我真的想不出为什么我不想让那个人设置字符串的副作用,但我想让任何一个老年人得到字符串的副作用。真奇怪。
此外,我一生都想不起为什么我会想要这样的情况,而是把它宣传成一个nsstring。我是说为什么?为什么您的不可变属性有时是可变的,但只有当setter(您不控制它)也告诉您,但您不希望来自用设置的对象的副作用时。你不告诉任何人这件事,他们只能想办法?我的头疼。
我猜这就是为什么苹果做了它做的事情。但这只是猜测。
此外,如果您可以摆脱不可变的数据类型,您应该。它减少了各种复杂性,特别是多线程。
当然,objective-c(以及大多数oop语言)允许您在真正需要的时候执行所有这些操作。这取决于你的程序员和你的团队。你也可以用手机撞击你的头骨来测试加速度计。不过,我不认为苹果推荐这两种方法。

关于objective-c - 属性中的(copy)属性是否有实际意义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21512812/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com