Objective C Singleton 类“已定义但未使用”警告!
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您将静态变量移至实现 (
.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.