gpt4 book ai didi

ios - 使一个属性强,非原子的objective-c

转载 作者:行者123 更新时间:2023-12-01 19:40:10 25 4
gpt4 key购买 nike

我有一个多 View 应用程序并使用一个对象来跟踪我的登录用户。我的 User.h 看起来像这样

@interface User : NSObject

@property (strong, nonatomic) NSDictionary *data;

@property (weak, nonatomic) NSString *uid;
@property (weak, nonatomic) NSString *firstName;
@property (weak, nonatomic) NSString *lastName;
@property (weak, nonatomic) NSString *dob;
@property (weak, nonatomic) NSString *gender;
@property (weak, nonatomic) NSString *avatarURL;
@property (assign, nonatomic) NSInteger status;

- (void)setPropertiesWith:(NSDictionary *)data;

User.m 看起来像这样
#import "User.h"

@implementation User
/*
* set properties
*/
- (void)setPropertiesWith:(NSDictionary *)data{
self.data = data;

self.uid = self.data[@"uid"];
self.firstName = self.data[@"firstName"];
self.lastName = self.data[@"lastName"];
self.dob = self.data[@"dob"];
self.gender = self.data[@"gender"];
self.status = [[self.data valueForKeyPath:@"status"] intValue];
self.avatarURL = self.data[@"avatarURL"];
}

@end

我的数据很弱,但在其中一种观点中它会变成空 - 我相信 ARC 正在发布它。如果我错了,请纠正我。

我有两个问题:
  • 使用此设置,数据为 strong其余属性为 weak ,这有什么潜在的风险吗?
  • 我应该将数据设为 ivar 并保持其余部分不变吗?

  • 这些属性的存在没有实际原因(除了我糟糕的类(class)设计技巧)。我只是觉得这很有趣,并想了解发生了什么。

    最佳答案

    您询问:

    1. With this setup, the data being strong and the rest of the properties being weak, is there any potential risk to this?


    是的,如果您 nil dictionary ,您的所有属性都可能变为 nil ,假设您在其他地方没有对它们的其他强引用。

    1. Should I make the data an ivar and keep the rest as is?


    我什至不会把它变成一个 ivar(除非有一些其他的要求来保存你没有与我们分享的这个)。它应该只是一个局部变量,并使您的属性 copy (或 strong )。

    我建议 (a) 摆脱 NSDictionary属性(property)和 (b) 制作 NSString属性为 copy (或 strong ),而不是 weak .此外,而不是拥有 setPropertiesWith方法,我只定义一个初始化器:
    // User.h

    @interface User : NSObject

    @property (copy, nonatomic) NSString *uid;
    @property (copy, nonatomic) NSString *firstName;
    @property (copy, nonatomic) NSString *lastName;
    @property (copy, nonatomic) NSString *dob;
    @property (copy, nonatomic) NSString *gender;
    @property (copy, nonatomic) NSString *avatarURL;
    @property (assign, nonatomic) NSInteger status;

    - (instancetype)initWithDictionary:(NSDictionary *)dictionary;

    @end


    // User.m

    @implementation User

    - (instancetype)initWithDictionary:(NSDictionary *)dictionary {
    if ((self = [super init])) {
    self.uid = dictionary[@"uid"];
    self.firstName = dictionary[@"firstName"];
    self.lastName = dictionary[@"lastName"];
    self.dob = dictionary[@"dob"];
    self.gender = dictionary[@"gender"];
    self.status = [dictionary[@"status"] intValue];
    self.avatarURL = dictionary[@"avatarURL"];
    }

    return self;
    }

    @end

    然后,调用者会这样做:
    User *user = [[User alloc] initWithDictionary:someDictionary];

    您可以在此处考虑其他改进(例如 readonly 公共(public)接口(interface)、声明可空性、字典上的轻量级泛型等),但以上可能是一个很好的起点。

    顺便说一句,如果你想知道我为什么制作这些 copy而不是 strong ,我们只是想保护自己,以防调用者通过 NSMutableString (这是一个 NSString 子类),后来意外地对其进行了变异。这只是更安全一点,更具防御性的模式。

    关于ios - 使一个属性强,非原子的objective-c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54896104/

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