简体   繁体   中英

Objective-C setting/accessing iVars in singleton class' superclass

I've got two classes: A superclass, User and its subclass, UserSession. User is extremely simple, just a container for some instanceVariables:

//User.h    
@interface User : NSObject

- (id)initWithJSON:(NSDictionary *)JSONDictionary;

-(NSNumber *)getUserID;
-(NSString *)getUserName;
-(NSString *)getUserUsername;
-(NSString *)getUserEmail;
-(NSString *)getUserBio;
-(void)setUserID:(NSNumber *)userID;
-(void)setUserName:(NSString *)userName;
-(void)setUserUsername:(NSString *)userUsername;
-(void)setUserEmail:(NSString *)userEmail;
-(void)setUserBio:(NSString *)userBio;
@end

and

//User.m
@interface User()

@end

@implementation User

NSNumber *_userID;
NSString *_userName;
NSString *_userUsername;
NSString *_userEmail;
NSString *_userBio;

- (id)initWithJSON:(NSDictionary *)JSONDictionary
{
    self = [super init];
    if (!self)
        return nil;

    _userID = JSONDictionary[@"userID"];
    _userName = JSONDictionary[@"userName"];
    _userUsername = JSONDictionary[@"userUsername"];
    _userEmail = JSONDictionary[@"userEmail"];
    _userBio = JSONDictionary[@"userBio"];

    return self;
}

plus associated setters and getters for the iVars.

UserSession is a singleton class that subclasses User:

//UserSession.m
@interface UserSession ()

@end

@implementation UserSession

static UserSession *_session = nil;


-(void)updateUserForUserSessionWithParams:(NSDictionary *)params
{
    [self setUserID:params[@"userId"]];
    [self setUserName:params[@"userName"]];
    [self setUserUsername:params[@"userUsername"]];
    [self setUserEmail:params[@"userEmail"]];
    [self setUserBio:params[@"userBio"]];
}

+ (instancetype)sharedSession
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // Create a session
        _session = [[UserSession alloc] init];
    });
    return _session;
}

Heres the problem: when I make calls like

[[UserSession sharedSession]
 updateUserForUserSessionWithParams:@{@"userId":@1],
                      @"userName":@"Steve Smith",
                      @"userUsername":responseObject[@"ssmith"],
                      @"userEmail":responseObject[@"ssmith@test.com"],
                      @"userBio":responseObject[@"I'm steve smith"]}];

and then elsewhere:

NSString* stringName = [[UserSession sharedSession] getUserName];
NSString* stringUsername = [[UserSession sharedSession] getUserUsername];
NSString* stringBio = [[UserSession sharedSession] getUserBio];

these getters return nil!

Sorry for all the code, but I think I'm doing something dumb wrong. Any help would be amazing.

Thanks in advance!

I don't have a -[UserSession init] though. What would I put in there?

Well, for starters a subclass should always call it's superclass's initializer, so you should call -initWithJSON: or some other User initializer. That won't entirely solve your problem in this case, but Objective-C relies heavily on conventions to make things work and it's best to stick to those conventions unless you really understand what you're doing.

I think the main problem with your code is here:

@implementation User

NSNumber *_userID;
NSString *_userName;
NSString *_userUsername;
NSString *_userEmail;
NSString *_userBio;

Those aren't instance variables you're declaring -- they're file-scoped global variables. You can declare instance variables as part of your @implementation block, but you still need the braces like you do for an @interface block:

@implementation User
{
    NSNumber *_userID;
    NSString *_userName;
    NSString *_userUsername;
    NSString *_userEmail;
    NSString *_userBio;
}
//...

I'd recommend using properties for these anyway. It's easy to get accessor methods wrong, and finding those kinds of problems (as in this case) usually isn't easy if you're just starting out. It might not be easy even if you have a lot of experience, but that's hard to know; people with a lot of experience usually use properties.

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