"weak" 和 "copy" 属性 属性 完全自定义 getter 和 setter

"weak" and "copy" attributes for property with full custom getter and setter

我是 Objective-C 的初学者,所以我尝试从这个 post 完成 Apple 练习:Apple Objective-C guide

我正在尝试使用自定义 getter 和 setter 创建 class 属性,一个具有“弱”属性,一个具有“复制”属性:

@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];

所以我希望在 name 设置为 nil 之后,weak 属性 也变成了 nil;标记为“copy”的 属性 将处理它们自己的初始字符串副本,并且将一些值附加到初始字符串不会对 MyPerson 对象造成任何后果。但是,当我调用日志方法时,我有 firstName 的原始值和 secondName 的更改值,就像没有“弱”和“复制”属性一样。

这是我要写的日志:

First name: John; Second name: Doe; BirthDate: (null)

First name: John; Second name: Doe; BirthDate: (null)

First name: John; Second name: Doedze; BirthDate: (null)

我期待这样的事情:

First name: John; Second name: Doe; BirthDate: (null)

First name: Not specified; Second name: Doe; BirthDate: (null)

First name: Not specified; Second name: Doe; BirthDate: (null)

我知道在第二行 GC 不能破坏 firstName 的值,但是第三次​​调用 log 方法在 1 秒睡眠后执行。我认为 1 秒足以让 GC 收集未使用的字符串。

知道为什么可以忽略“weak”和“copy”属性吗? 感谢您的帮助。

完整代码: 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

知道为什么可以忽略“weak”和“copy”属性吗?

Any idead why "weak" and "copy" attributes can be ignored? Thanks for help.

他们没有被忽视。你的测试有缺陷。

  • 要测试 "weak",您需要使用 NSString 以外的东西,因为字符串有特殊的内存管理。使用没有变量引用的 NSObject;一行之后就会烟消云散

    self.ob = [NSObject new];
    NSLog(@"%@", self.ob); // nil!
    
  • 要测试"copy",您需要从一个NSMutableString 开始,保留对它的引用,将其分配给属性,然后改变可变字符串;分配的字符串不会改变,证明 属性 没有持有对同一个可变字符串的另一个引用。

  • 要测试内存管理属性(如copy)以及线程属性(如atomic),请执行not写你自己的 setter!这些是编译器关于 it 应该如何编写 setter(综合)的说明。如果您手动编写 setter,则不会得到综合,此时您的属性将毫无意义(除非它们告知客户您的 API)。