我测试了这个示例代码。
样本.h
{
NSString *string1;
NSString *string2;
NSMutableString *string3;
}
@property (assign) IBOutlet NSWindow *window;
@property (strong,nonatomic) NSString *string1;
@property (copy,nonatomic) NSString *string2;
@property (strong,nonatomic) NSMutableString *string3;
@end
样品.m
#import "Sample.h"
@synthesize string1;
@synthesize string2;
@synthesize string3;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
string1 = [[NSString alloc] init];
string2 = [[NSString alloc] init];
string3 = [[NSMutableString alloc] initWithString:@"test"];
string2 = string3;
string1 = string3;
[string3 appendString:@"test"];
NSLog(@"%@",string1);
NSLog(@"%@",string2);
NSLog(@"%@",string3);
}
@end
结果是
2012-09-23 00:11:48.610 sample[13468:303] testtest
2012-09-23 00:11:48.611 sample[13468:303] testtest
2012-09-23 00:11:48.611 sample[13468:303] testtest
我认为 string2 应该是“测试”,因为该属性是副本。但 string2 是“testtest”。
string2 = [string3 copy];
这是我认为的结果。
string2 is "test"
为什么?请告诉我,我睡不好。