Objective-C。属性的强 vs 副本。未能收到预期结果
Objective-C. strong vs copy for properties. Fail to receive expected result
我对 Objective-C 很陌生。我正在阅读 Paul Hudson 的书 Objective-C for Swift Developers。
在书中,在 类 章节和 属性 属性 下,我找到了这个:
Some of the attributes bear more explanation. For example, why use copy
rather than strong
? If you create an NSMutableString
and assign it to a strong
property of two different objects, both properties point to the same mutable string. So, if one changes, they both change. This might be what you want in some instances - for example, arrays - but if you want each object to have its own unique properties that cant be changed by surprise, you should use copy
instead
只是为了测试一下,这是我尝试做的。
我有一个 class Person2
有一个方法 (printGreetingsForAllKubas
) 来打印分别分配给 3 个不同属性的 3 个名称。
NSString
nameProperty
具有默认属性
NSString
namePropertySame
也有默认属性
NSString
namePropertyCopied
我给了一个 copy
属性
然后,我在我的主要实现文件中做了这个。
NSMutableString *mutableName = [NSMutableString stringWithString:@"Kuba"];
person2.nameProperty = mutableName;
person2.namePropertySame = mutableName;
person2.namePropertyCopied = mutableName;
[person2 printGreetingsForAllKubas];
mutableName = [NSMutableString stringWithString:@"John"];
NSLog(@"%@", mutableName);
[person2 printGreetingsForAllKubas];
我所期望的是首先打印:
Hello Jakub, and Jakub, and Jakub
第二次,在我将 mutableName 更改为 @"John"
之后,我预计
Hello John, and John, and Jakub
但是打印出来了
Hello Jakub, and Jakub, and Jakub
如果有人能解释我做错了什么,我将不胜感激。我将如何编写更改 mutableName
值会影响打印方法结果的代码?
非常感谢:))
考虑:
mutableName = [NSMutableString stringWithString:@"John"];
这不会改变对象。这会更新本地 mutableName
变量以指向一个全新的 NSMutableString
实例。因此,提供给 person2
的可变字符串将保持不变,因为您没有改变该对象,而是创建了一个新对象。
如果您想体现作者所说的行为,应该改变原始 NSMutableString
对象,例如,更改其 string
属性,例如
mutableName.string = @"John";
这会改变现有的 NSMutableString
对象,而不是创建新实例。在这种情况下,具有 strong
属性的 person2
属性将反映突变(根据您引用的警告说明),但具有 copy
属性的 person2
属性将不会有它们的变化价值观在他们背后改变了。
我对 Objective-C 很陌生。我正在阅读 Paul Hudson 的书 Objective-C for Swift Developers。
在书中,在 类 章节和 属性 属性 下,我找到了这个:
Some of the attributes bear more explanation. For example, why use
copy
rather thanstrong
? If you create anNSMutableString
and assign it to astrong
property of two different objects, both properties point to the same mutable string. So, if one changes, they both change. This might be what you want in some instances - for example, arrays - but if you want each object to have its own unique properties that cant be changed by surprise, you should usecopy
instead
只是为了测试一下,这是我尝试做的。
我有一个 class Person2
有一个方法 (printGreetingsForAllKubas
) 来打印分别分配给 3 个不同属性的 3 个名称。
NSString
nameProperty
具有默认属性
NSString
namePropertySame
也有默认属性
NSString
namePropertyCopied
我给了一个 copy
属性
然后,我在我的主要实现文件中做了这个。
NSMutableString *mutableName = [NSMutableString stringWithString:@"Kuba"];
person2.nameProperty = mutableName;
person2.namePropertySame = mutableName;
person2.namePropertyCopied = mutableName;
[person2 printGreetingsForAllKubas];
mutableName = [NSMutableString stringWithString:@"John"];
NSLog(@"%@", mutableName);
[person2 printGreetingsForAllKubas];
我所期望的是首先打印:
Hello Jakub, and Jakub, and Jakub
第二次,在我将 mutableName 更改为 @"John"
之后,我预计
Hello John, and John, and Jakub
但是打印出来了
Hello Jakub, and Jakub, and Jakub
如果有人能解释我做错了什么,我将不胜感激。我将如何编写更改 mutableName
值会影响打印方法结果的代码?
非常感谢:))
考虑:
mutableName = [NSMutableString stringWithString:@"John"];
这不会改变对象。这会更新本地 mutableName
变量以指向一个全新的 NSMutableString
实例。因此,提供给 person2
的可变字符串将保持不变,因为您没有改变该对象,而是创建了一个新对象。
如果您想体现作者所说的行为,应该改变原始 NSMutableString
对象,例如,更改其 string
属性,例如
mutableName.string = @"John";
这会改变现有的 NSMutableString
对象,而不是创建新实例。在这种情况下,具有 strong
属性的 person2
属性将反映突变(根据您引用的警告说明),但具有 copy
属性的 person2
属性将不会有它们的变化价值观在他们背后改变了。