简体   繁体   中英

making one property strong,nonatomic for objective-c

I have a multi view application and use an object to keep track of my logged in user. My User.h looks like this

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

And the User.m looks like this

#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

I had the data as weak, but in one of the views it would turn up null - I believe ARC was releasing it. Please correct me if I am wrong.

I have 2 questions:

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

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

There is no actual reason(other than my poor class design skills) for the existence of the properties. I just find it very interesting and wanted to understand what is going on.

You asked:

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

Yes, if you nil the dictionary , all of your properties would likely become nil , assuming you don't have other strong references to them elsewhere.

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

I wouldn't even make it an ivar (unless there's some other requirement for saving this that you haven't shared with us). It should just be a local variable, and make your properties copy (or strong ).


I'd suggest (a) getting rid of the NSDictionary property and (b) making the NSString properties be copy (or strong ), not weak . Also, rather than having a setPropertiesWith method, I'd just define an initializer:

// 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

And

// 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

And then, the caller would do:

User *user = [[User alloc] initWithDictionary:someDictionary];

There are other refinements you could consider here (eg readonly public interface, declaring nullability, lightweight generics on the dictionary, etc.), but the above is probably a good starting point.


By the way, if your wondering why I made these copy instead of strong , we just want to protect ourselves in case the caller passed a NSMutableString (which is a NSString subclass) and accidentally mutated it later. This is just a bit safer, a little more defensive pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM