gpt4 book ai didi

ios - 具有完全自定义 getter 和 setter 的属性的 "weak"和 "copy"属性

转载 作者:行者123 更新时间:2023-12-03 16:57:12 27 4
gpt4 key购买 nike

我是 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];

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

这是我的日志:

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秒 sleep 后执行。我认为1秒足够GC收集未使用的字符串的时间。

知道为什么“弱”和“复制”属性可以被忽略吗?感谢您的帮助。

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

有什么想法可以忽略“弱”和“复制”属性吗?

最佳答案

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

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

  • 要测试“weak”,您需要使用 NSString 以外的其他东西,因为字符串具有特殊的内存管理。使用没有变量引用的 NSObject;一行之后它就会化为一缕烟雾消失。

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

  • 要测试内存管理属性(例如copy)以及线程属性(例如atomic),请勿编写你自己的二传手!这些是编译器应如何编写 setter(综合)的说明。如果您手动编写 setter,则无法进行综合,此时您的属性就毫无意义(除非它们通知客户端您的 API)。

关于ios - 具有完全自定义 getter 和 setter 的属性的 "weak"和 "copy"属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27946906/

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