Singleton 在 XCODE 中不起作用

发布于 2024-11-27 01:29:44 字数 1541 浏览 0 评论 0原文

我正在尝试在我的 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 技术交流群。

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

发布评论

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

评论(3

望她远 2024-12-04 01:29:44

以下代码片段直接来自 Apple 工程师,目前被认为(内部)是实现单例的最佳且最有效的方法(因为它充分利用了 GCD em>) ...

+(id)sharedInstance
{
    static dispatch_once_t pred = 0;
    static id object = nil;
    dispatch_once(&pred, ^{ object = /* object initialization goes here */ });
    return object;
}

引用上述工程师关于上述内容的内容...

简短、甜蜜,并且在当前架构上合法的任何内容都可能已被用于实现,这意味着您永远不必担心当前内存模型中什么是合法的或不合法的。

它还允许您摆脱其他过时的实现所需的所有样板代码。

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) ...

+(id)sharedInstance
{
    static dispatch_once_t pred = 0;
    static id object = nil;
    dispatch_once(&pred, ^{ object = /* object initialization goes here */ });
    return object;
}

Quote from said engineer regarding the above ...

Short, sweet, and whatever happens to be legal on the current architecture is likely to have been taken advantage of for the implementation, which means you never have to worry about what is or isn't legal in your current memory model.

It also allows you take get rid of all the boilerplate code needed by other, out-of-date implementations.

春庭雪 2024-12-04 01:29:44

实现此处给出的其余方法并尝试。

编辑:

您是否保留 myToken 字符串变量?

-(void)setAuth:(NSString*) token {
    if( token != myToken )
    {
       [myToken release];
       myToken=[token copy];
    }
}

像这样替换 setAuth 方法..

Implemet the rest of the methods also as given here and try.

Edit:

Are u retaining the myToken string variable?

-(void)setAuth:(NSString*) token {
    if( token != myToken )
    {
       [myToken release];
       myToken=[token copy];
    }
}

Replace the setAuth method like this..

叹梦 2024-12-04 01:29:44

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!

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