Objective C Singleton 类“已定义但未使用”警告!

发布于 2024-11-01 10:04:03 字数 1082 浏览 2 评论 0原文

我正在使用 Singleton 类,以下是代码:

.h 文件:  

#import <Foundation/Foundation.h>


@interface Credential : NSObject {
    NSString *UID;
    NSString *UPASS;


}

@property(nonatomic,retain) NSString *UID;
@property(nonatomic,retain) NSString *UPASS;


static Credential *credential = NULL;

+(Credential*) sharedInstance;

/*
+ @property(nonatomic,retain) NSString *UID;
+ @property(nonatomic,retain) NSString *UPASS;
*/

@end

.m 文件:

#import "Credential.h"


@implementation Credential

@synthesize UID,UPASS;

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

+(Credential*) sharedInstance
{
    @synchronized(self)
    {
        if (credential == NULL) {
            credential = [[Credential alloc] init];
        }
    }
    return credential;
}

@end

以下行产生警告“已定义但未使用”

   static Credential *credential = NULL;

我无法弄清楚我一直在“sharedInstance”函数下的 .m 文件中使用凭据变量,那么为什么我会得到这个警告?

对我来说这是一个奇怪的问题!

I'm using a Singleton class and following is the code:

.h File: 

#import <Foundation/Foundation.h>


@interface Credential : NSObject {
    NSString *UID;
    NSString *UPASS;


}

@property(nonatomic,retain) NSString *UID;
@property(nonatomic,retain) NSString *UPASS;


static Credential *credential = NULL;

+(Credential*) sharedInstance;

/*
+ @property(nonatomic,retain) NSString *UID;
+ @property(nonatomic,retain) NSString *UPASS;
*/

@end

.m file:

#import "Credential.h"


@implementation Credential

@synthesize UID,UPASS;

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

+(Credential*) sharedInstance
{
    @synchronized(self)
    {
        if (credential == NULL) {
            credential = [[Credential alloc] init];
        }
    }
    return credential;
}

@end

The following line produces warning "defined but not used"

   static Credential *credential = NULL;

I couldn't figure out that I've been using credential variable in .m file under "sharedInstance" function then why am I getting this warning?

A strange issue to me!

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

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

发布评论

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

评论(1

夜雨飘雪 2024-11-08 10:04:03

当您将静态变量移至实现 (.m) 文件的顶部时,问题会消失吗?与此相关的是,我认为您会受益于摆脱单身< /a> 总共。

Does the problem go away when you move the static variable to the top of the implementation (.m) file? And on a related note, I think that you would benefit from getting rid of the singleton altogether.

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