Singleton 在 XCODE 中不起作用
我正在尝试在我的 iPhone 应用程序中运行单例。
当我尝试实现并从第一个类向单例类请求它时,它正在运行,但是之后,当我尝试从其他类使用它时,它不起作用......
这是对单例中的调用第一类和第二类:
NSLog([ [MySingleton sharedMySingleton] getAuth]);
她是单例类:
#import "MySingleton.h"
@implementation MySingleton
static MySingleton* _sharedMySingleton = nil;
@synthesize myToken;
+(MySingleton*)sharedMySingleton
{
@synchronized([MySingleton class])
{
if (!_sharedMySingleton)
[[self alloc] init];
return _sharedMySingleton;
}
return nil;
}
+(id)alloc
{
@synchronized([MySingleton class])
{
NSAssert(_sharedMySingleton == nil, @"Attempted to allocate a second instance of a singleton.");
_sharedMySingleton = [super alloc];
return _sharedMySingleton;
}
return nil;
}
-(id)init {
self = [super init];
if (self != nil) {
myToken = [[NSString alloc] initWithString:@""];
}
return self;
}
-(void)setAuth:(NSString*) token {
myToken=token;
}
-(NSString*)getAuth {
return myToken;
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)retain {
return self;
}
- (unsigned)retainCount {
return UINT_MAX; //denotes an object that cannot be released
}
- (void)release {
// never release
}
- (id)autorelease {
return self;
}
- (void)dealloc {
// Should never be called, but just here for clarity really.
[myToken release];
[super dealloc];
}
@end
我在第二类中正确导入了单例类;-)
就是这样!
谢谢你的帮助:-D
I'm trying to run a singleton in my iPhone App'.
When I try to implement and to request it from the first class to the singleton's class, it's running but after, when I try to use it from an other class, it doesn't working...
Here's the call to the singleton in the first and second classes:
NSLog([ [MySingleton sharedMySingleton] getAuth]);
Her's the Singleton's class :
#import "MySingleton.h"
@implementation MySingleton
static MySingleton* _sharedMySingleton = nil;
@synthesize myToken;
+(MySingleton*)sharedMySingleton
{
@synchronized([MySingleton class])
{
if (!_sharedMySingleton)
[[self alloc] init];
return _sharedMySingleton;
}
return nil;
}
+(id)alloc
{
@synchronized([MySingleton class])
{
NSAssert(_sharedMySingleton == nil, @"Attempted to allocate a second instance of a singleton.");
_sharedMySingleton = [super alloc];
return _sharedMySingleton;
}
return nil;
}
-(id)init {
self = [super init];
if (self != nil) {
myToken = [[NSString alloc] initWithString:@""];
}
return self;
}
-(void)setAuth:(NSString*) token {
myToken=token;
}
-(NSString*)getAuth {
return myToken;
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)retain {
return self;
}
- (unsigned)retainCount {
return UINT_MAX; //denotes an object that cannot be released
}
- (void)release {
// never release
}
- (id)autorelease {
return self;
}
- (void)dealloc {
// Should never be called, but just here for clarity really.
[myToken release];
[super dealloc];
}
@end
I imported correctly the singleton's class in the second class ;-)
That's it!
thanks for your help :-D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下代码片段直接来自 Apple 工程师,目前被认为(内部)是实现单例的最佳且最有效的方法(因为它充分利用了 GCD em>) ...
引用上述工程师关于上述内容的内容...
它还允许您摆脱其他过时的实现所需的所有样板代码。
The following snippet has come direct from an Apple engineer and is currently regarded (in-house) as the best and most efficient way to implement a singleton (as it takes full advantage of GCD) ...
Quote from said engineer regarding the above ...
It also allows you take get rid of all the boilerplate code needed by other, out-of-date implementations.
实现此处给出的其余方法并尝试。
编辑:
您是否保留 myToken 字符串变量?
像这样替换 setAuth 方法..
Implemet the rest of the methods also as given here and try.
Edit:
Are u retaining the myToken string variable?
Replace the setAuth method like this..
stackoverflow 上有一个关于单例的完美主题。我什至认为我自己不应该提出任何其他建议。只需检查一下:
我的 Objective-C 单例应该是什么样子?
这对你来说一定是绰绰有余了。祝你好运!
There's a perfect topic on stackoverflow about singletons. I don't think even that I should make any other suggestions from myself. Just inspect this:
What should my Objective-C singleton look like?
It must be really more than enough for you. Good luck!