我是Objective-C的初学者,所以我尝试从这篇文章中完成Apple练习:Apple Objective-C guide
我尝试使用自定义getter和setter创建类属性,一个具有“weak”属性,另一个具有“copy”属性:
@property (weak, getter=getFirstName, setter=setFirstName:) NSString *firstName;
@property (copy, getter=getFirstName, setter=setFirstName:) NSString *secondName;
然后,我测试这样的属性:
NSString *name = @"John";
NSMutableString *surname = [NSMutableString stringWithString:@"Doe"];
MyPerson *person = [MyPerson createWithFirstName:name secondName:surname];
[person tellName];
name = nil;
[person tellName];
[NSThread sleepForTimeInterval:1.0f];
[surname appendString:@"dze"];
[person tellName];
所以我希望在名字设定为零后,弱财产也变成零;标记为“copy”的属性将处理它们自己的初始字符串副本,并将一些值附加到初始字符串不会对MyPerson对象造成任何后果。但是,当我调用log方法时,我在firstName中有原始值并在secondName中更改了值,就像没有“weak”和“copy”属性一样。
这是我记录的日志:
名字:约翰;第二个名字:Doe; BirthDate :( null)
名字:约翰;第二个名字:Doe; BirthDate :( null)
名字:约翰;第二个名字:Doedze; BirthDate :( null)
我期待这样的事情:
名字:约翰;第二个名字:Doe; BirthDate :( null)
名字:未指定;第二个名字:Doe; BirthDate :( null)
名字:未指定;第二个名字:Doe; BirthDate :( null)
据我所知,在第二行GC不能破坏firstName的值,但是第三次调用log方法在1秒睡眠后执行。我认为1秒足以让GC收集未使用的字符串。
任何idead为什么可以忽略“弱”和“复制”属性? 谢谢你的帮助。
完整代码: 的main.m:
#import <Foundation/Foundation.h>
#import "MyPerson.h"
int main(int argc, const char * argv[])
{
@autoreleasepool
{
NSString *name = @"John";
NSMutableString *surname = [NSMutableString stringWithString:@"Doe"];
MyPerson *person = [MyPerson createWithFirstName:name secondName:surname];
[person tellName];
name = nil;
[person tellName];
[NSThread sleepForTimeInterval:1.0f];
[surname appendString:@"dze"];
[person tellName];
}
return 0;
}
MyPerson.h:
#import <Foundation/Foundation.h>
@interface MyPerson : NSObject
@property (weak, getter=getFirstName, setter=setFirstName:) NSString *firstName;
@property (copy, getter=getSecondName, setter=setSecondName:) NSString *secondName;
@property (getter=getBirthDate, setter=setBirthDate:) NSDate *birthDate;
-(void)setFirstName:(NSString *)firstName;
-(NSString *)getFirstName;
-(void)setSecondName:(NSString *)secondName;
-(NSString *)getSecondName;
-(void)setBirthDate:(NSDate *)birthDate;
-(NSDate *)getBirthDate;
-(void) tellName;
+(id)create;
+(id)createWithFirstName:(NSString *)firstName;
+(id)createWithFirstName:(NSString *)firstName secondName:(NSString *)secondName;
+(id)createWithFirstName:(NSString *)firstName secondName:(NSString *)secondName birthDate:(NSDate *)birthDate;
-(id)initWithFirstName:(NSString *)firstName secondName:(NSString *)secondName birthDate:(NSDate *)birthDate;
@end
MyPerson.m:
#import "MyPerson.h"
@implementation MyPerson
@synthesize firstName = m_firstName;
@synthesize secondName = m_secondName;
@synthesize birthDate = m_birthDate;
-(void)tellName
{
NSMutableString *stringBuilder = [NSMutableString string];
[stringBuilder appendString:@"First name: "];
[self appendIfNotNil:self.firstName toBuilder:stringBuilder];
[stringBuilder appendString:@"; "];
[stringBuilder appendString:@"Second name: "];
[self appendIfNotNil:self.secondName toBuilder:stringBuilder];
[stringBuilder appendString:@"; "];
[stringBuilder appendString:@"BirthDate: "];
[stringBuilder appendFormat:@"%@", [m_birthDate descriptionWithLocale:NSLocaleLanguageDirectionUnknown]];
NSLog(@"%@", stringBuilder);
}
-(void)appendIfNotNil:(NSString*)str toBuilder:(NSMutableString *)stringBuilder
{
[stringBuilder appendString:str == nil ? @"Not specified" : str];
}
-(void)setFirstName:(NSString *)firstName
{
NSLog(@"Setter called for first name: %@", firstName);
m_firstName = firstName;
}
-(NSString *)getFirstName
{
NSLog(@"Getter called for first name");
return m_firstName;
}
-(void)setSecondName:(NSString *)secondName
{
NSLog(@"Setter called for second name: %@", secondName);
m_secondName = secondName;
}
-(NSString *)getSecondName
{
NSLog(@"Getter called for second name");
return m_secondName;
}
-(id)initWithFirstName:(NSString *)firstName secondName:(NSString *)secondName birthDate:(NSDate *)birthDate
{
self = [super init];
if (self)
{
m_firstName = firstName;
m_secondName = secondName;
m_birthDate = birthDate;
}
return self;
}
+(id)createWithFirstName:(NSString *)firstName secondName:(NSString *)secondName birthDate:(NSDate *)birthDate
{
MyPerson *person = [MyPerson alloc];
person = [person initWithFirstName:firstName secondName:secondName birthDate:birthDate];
return person;
}
+(id)createWithFirstName:(NSString *)firstName secondName:(NSString *)secondName
{
return [MyPerson createWithFirstName:firstName secondName:secondName birthDate:nil];
}
+(id)createWithFirstName:(NSString *)firstName
{
return [MyPerson createWithFirstName:firstName secondName:nil birthDate:nil];
}
+(id)create
{
return [MyPerson createWithFirstName:nil secondName:nil birthDate:nil];
}
@end
为什么“弱”和“复制”属性可以被忽略?
答案 0 :(得分:3)
任何想法,为什么&#34;弱&#34;和&#34;复制&#34;属性可以忽略吗?谢谢你的帮助。
他们不被忽视。你的测试存在缺陷。
要测试&#34; weak&#34;,您需要使用NSString以外的其他内容,因为字符串具有特殊的内存管理。使用没有变量引用的NSObject;它会在一阵烟雾中消失。
self.ob = [NSObject new];
NSLog(@"%@", self.ob); // nil!
要测试&#34; copy&#34;,您需要从NSMutableString开始,保持对它的引用,将其分配给属性,然后改变可变字符串;分配的字符串不会改变,证明该属性没有持有对同一可变字符串的另一个引用。
要测试内存管理属性(如copy
)以及线程属性(如atomic
),请不编写自己的setter!这些是编译器关于 it 应该如何编写setter(合成)的指令。如果您手动编写setter,则无法获得综合,此时您的属性毫无意义(除非他们告知客户您的API)。