单例实现的问题

发布于 2024-11-09 09:22:17 字数 1555 浏览 2 评论 0原文

我在单例实现方面遇到了问题。看来我想保存在单身人士中的一个物品被损坏了,我不明白为什么。任何帮助表示赞赏。

这是单例的代码: SessionServices.h

    #import <Foundation/Foundation.h>

/**
 This class provides a simple way of getting information about the connected user
*/
@class  UserIHM;
@interface SessionServices : NSObject {
    @private
    UserIHM *user; //the object to retain
}

@property (nonatomic, retain) UserIHM *user;

sessionServices.m

@implementation SessionServices
@synthesize user;

static SessionServices *INSTANCE = nil;

+ (SessionServices*)sharedInstance
{
    if (INSTANCE == nil) {
        INSTANCE = [[super allocWithZone:NULL] init];
    }
    return INSTANCE;
}
....
//singleton impl from apple documentation
...
}

userIHM.h

@interface UserIHM : NSObject {
    @private
    NSString *tagUID;
    NSString *username;
    BOOL isAdmin;
}

@property (nonatomic,retain) NSString *tagUID;
@property (nonatomic,retain) NSString *username;
@property (nonatomic) BOOL isAdmin;

然后在 SessionServices.m 中我调用:

user = [[IHMObjectFinderServices sharedInstance] getUserByTagUID:userTagUID];

并且用户的所有字段都填充了正确的信息。

塔吉德 = 2ac6912a 用户名 = 迈克 isAdmin = NO

然后我尝试使用此信息来设置我的 UITableView 的标题,

self.navigationItem.title = [NSString stringWithFormat:@"Projects: %@",[[[SessionServices sharedInstance] user] username]];

如果我 NSLog 并使用调试器,我可以看到用户名变成

无效的 CFString

我做错了什么?

I've got a problem with a singleton implementation. It seems an objet I want to hold in my singleton gets corrupted and I can't figure why. Any help appreciated.

Here is the code of the singleton:
SessionServices.h

    #import <Foundation/Foundation.h>

/**
 This class provides a simple way of getting information about the connected user
*/
@class  UserIHM;
@interface SessionServices : NSObject {
    @private
    UserIHM *user; //the object to retain
}

@property (nonatomic, retain) UserIHM *user;

sessionServices.m

@implementation SessionServices
@synthesize user;

static SessionServices *INSTANCE = nil;

+ (SessionServices*)sharedInstance
{
    if (INSTANCE == nil) {
        INSTANCE = [[super allocWithZone:NULL] init];
    }
    return INSTANCE;
}
....
//singleton impl from apple documentation
...
}

userIHM.h

@interface UserIHM : NSObject {
    @private
    NSString *tagUID;
    NSString *username;
    BOOL isAdmin;
}

@property (nonatomic,retain) NSString *tagUID;
@property (nonatomic,retain) NSString *username;
@property (nonatomic) BOOL isAdmin;

then in SessionServices.m I call:

user = [[IHMObjectFinderServices sharedInstance] getUserByTagUID:userTagUID];

and all the fields of the user get filled with correct info.

taguid = 2ac6912a
username = Mike
isAdmin = NO

then I try to use this info to set the title of my UITableView

self.navigationItem.title = [NSString stringWithFormat:@"Projects: %@",[[[SessionServices sharedInstance] user] username]];

if I NSLog and use the debugger, I can see that the username becomes an

invalid CFString

What am I doing wrong ?

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

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

发布评论

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

评论(2

七月上 2024-11-16 09:22:17

这个习惯用法稍微好一点:

+(SessionServices *)singleton {
    static dispatch_once_t pred;
    static SessionServices *shared = nil;
    dispatch_once(&pred, ^{
        shared = [[SessionServices alloc] init];
        // init your variables here
        shared.blah = blahblah;
    });
    return shared;
}

参见

This idiom is slightly better:

+(SessionServices *)singleton {
    static dispatch_once_t pred;
    static SessionServices *shared = nil;
    dispatch_once(&pred, ^{
        shared = [[SessionServices alloc] init];
        // init your variables here
        shared.blah = blahblah;
    });
    return shared;
}

See Care and Feeding of Singletons for an explanation.

薆情海 2024-11-16 09:22:17

根据您的评论,您遇到的问题取决于您没有在单例实现中初始化 *user ivar 的事实。

为此,请定义适当的 -init 方法。在这方面(初始化),单例的行为就像普通类一样。

- (id)init {
    self = [super init];
    if (self != nil) {
       user = [[UserIHM alloc] init];   //-- sergio: added alloc/init
       user.username = @"";
       user.tagUID = @"";
       user.isAdmin = NO;
    }
    return (self);
}

As per your comment, the problem you are having depends on the fact that you don't initialize your *user ivar in the singleton implementation.

To do that, define a proper -init method. In this respect (initialization), singletons behave like normal classes.

- (id)init {
    self = [super init];
    if (self != nil) {
       user = [[UserIHM alloc] init];   //-- sergio: added alloc/init
       user.username = @"";
       user.tagUID = @"";
       user.isAdmin = NO;
    }
    return (self);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文