单例实现的问题
我在单例实现方面遇到了问题。看来我想保存在单身人士中的一个物品被损坏了,我不明白为什么。任何帮助表示赞赏。
这是单例的代码: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个习惯用法稍微好一点:
参见
This idiom is slightly better:
See Care and Feeding of Singletons for an explanation.
根据您的评论,您遇到的问题取决于您没有在单例实现中初始化 *user ivar 的事实。
为此,请定义适当的
-init
方法。在这方面(初始化),单例的行为就像普通类一样。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.