无法为单例中的 BOOL 字段赋值

发布于 11-29 13:29 字数 2109 浏览 3 评论 0原文

I have this singleton:

UserData.h

@interface UserData : NSObject {
    User *user;
}

+ (UserData *)sharedUserData;

@property (nonatomic, retain) User *user;

UserData.m

@implementation UserData

@synthesize user;

SYNTHESIZE_SINGLETON_FOR_CLASS(UserData);

- (id) init
{
    self = [super init];
    if (self != nil) {
    }
    return self;
}

- (void) dealloc
{
    [user release];
    [super dealloc];
}

@end

User.h

@interface User : NSObject
{
    int user_id;
    NSString *username;
    BOOL logged_in;
}

@property (nonatomic, assign) int user_id;
@property (nonatomic, retain) NSString *username;
@property (nonatomic, assign, getter=isLogged) BOOL logged_in;

@end

User.m

@implementation User

@synthesize user_id, username, logged_in;

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

@end

in the app delegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window.rootViewController = self.tabBarController;

    BOOL logged_in = FALSE; // hardcoded for testing  

    UserData *userData = [UserData sharedUserData];
    [[userData user] setLogged_in:logged_in];

    if (!logged_in) {
        [self hideTabBar:self.tabBarController];
    }

    [self.window makeKeyAndVisible];
    return YES;
}

Up to here it works like a charm. The problem is when in the LoginViewController I change the value of logged_in:

BOOL logged_in = TRUE;    

UserData *userData = [UserData sharedUserData];
[[userData user] setLogged_in:logged_in];

if (userData.user.isLogged) {
    NSLog(@"You're logged in.");
} else {
    NSLog(@"You're not logged in.");
}

The debug console says me "You're not logged in".为什么会这样?怎么了?

I have this singleton:

UserData.h

@interface UserData : NSObject {
    User *user;
}

+ (UserData *)sharedUserData;

@property (nonatomic, retain) User *user;

UserData.m

@implementation UserData

@synthesize user;

SYNTHESIZE_SINGLETON_FOR_CLASS(UserData);

- (id) init
{
    self = [super init];
    if (self != nil) {
    }
    return self;
}

- (void) dealloc
{
    [user release];
    [super dealloc];
}

@end

User.h

@interface User : NSObject
{
    int user_id;
    NSString *username;
    BOOL logged_in;
}

@property (nonatomic, assign) int user_id;
@property (nonatomic, retain) NSString *username;
@property (nonatomic, assign, getter=isLogged) BOOL logged_in;

@end

User.m

@implementation User

@synthesize user_id, username, logged_in;

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

@end

in the app delegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window.rootViewController = self.tabBarController;

    BOOL logged_in = FALSE; // hardcoded for testing  

    UserData *userData = [UserData sharedUserData];
    [[userData user] setLogged_in:logged_in];

    if (!logged_in) {
        [self hideTabBar:self.tabBarController];
    }

    [self.window makeKeyAndVisible];
    return YES;
}

Up to here it works like a charm. The problem is when in the LoginViewController I change the value of logged_in:

BOOL logged_in = TRUE;    

UserData *userData = [UserData sharedUserData];
[[userData user] setLogged_in:logged_in];

if (userData.user.isLogged) {
    NSLog(@"You're logged in.");
} else {
    NSLog(@"You're not logged in.");
}

The debug console says me "You're not logged in". Why this? What's wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半枫2024-12-06 13:29:30
- (id) init
{
    self = [super init];
    if (self != nil) {
      self.user = [[User alloc] init];
    }
    return self;
}

- (id) init
{
    self = [super init];
    if (self != nil) {
      self.user = [[User alloc] init];
    }
    return self;
}

You also need to allocate memory for individual element of your singleton.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文