我有一个单身人士,我将数据传递给它,但它返回null,请你在我的情况下帮助我。在此先感谢:)
这是我的代码
Card.h
@property (weak,nonatomic) NSString *email;
@property (weak,nonatomic) NSString *fName;
@property (weak,nonatomic) NSString *lName;
@property (weak,nonatomic) NSString *category;
+(Card *)getCard;
Card.m
@synthesize email;
@synthesize fName;
@synthesize lName;
@synthesize category;
static csCard *instance;
+(Card *) getCard
{
@synchronized (self)
{
if(instance == nil)
{
instance = [[Card alloc]init];
}
}
return instance;
}
- (id) init{
self.email = [[NSUserDefaults standardUserDefaults]stringForKey:@"email"];
self.fName = [[NSUserDefaults standardUserDefaults]stringForKey:@"firstName"];
self.lName = [[NSUserDefaults standardUserDefaults]stringForKey:@"lastName"];
self.category = @"TestCategory";
return self;
}
这是我的测试代码,看看它是否正常工作
Test.m
Card *card = [Card getCard];
[card setEmail:self.emailField.text];
NSLog(@"%@",card.email);
但是这段代码给了我(null)
答案 0 :(得分:1)
像这样修改你的课程。
<强> Card.h 强>
@property (strong,nonatomic) NSString *email; //Let the modal be strong property
@property (strong,nonatomic) NSString *fName;
@property (strong,nonatomic) NSString *lName;
@property (strong,nonatomic) NSString *category;
+(Card *)getCard;
<强> Card.m 强>
static Card *instance;
+(Card *) getCard
{
@synchronized (self)
{
if(instance == nil)
{
instance = [[Card alloc]init];
}
}
return instance;
}
- (NSString)email{
return [[NSUserDefaults standardUserDefaults]stringForKey:@"email"];
}
- (void)setEmail:(NSString)email{
[[NSUserDefaults standardUserDefaults] setString:email forkey:@"email"];
}
无需覆盖init
在您的测试类中
Card *card = [Card getCard];
[card setEmail:self.emailField.text];
NSLog(@"%@",card.email);
答案 1 :(得分:0)
static csCard *instance;
+(csCard *) getCard
{
@synchronized (self)
{
if(instance == nil)
{
instance = [[csCard alloc]init];
}
}
return instance;
}
将其替换为此代码
static Card *instance;
+(Card *) getCard
{
@synchronized (self)
{
if(instance == nil)
{
instance = [[Card alloc]init];
}
}
return instance;
}
实例对象的类名是错误的,在单例方法中,返回数据类型也是错误的。我想你会明白我在说什么。
答案 2 :(得分:0)
在βhargavḯsujjested的帮助下,你可以修改你的代码,如下所示
在static csCard *instance;
行csCard
我正在使用 #import "Card.h"
static dispatch_once_t onceDispach;
@implementation Card
@synthesize email = _email;
@synthesize fName;
@synthesize lName;
@synthesize category;
static Card *instance = nil; //change this to Card because this instance which is of type Card
+(Card *)getCard
{
dispatch_once(&onceDispach, ^{
instance = [[self alloc] init];//careate Card shared object named instance
});
return instance;
}
- (id) init
{
self.email = [[NSUserDefaults standardUserDefaults]stringForKey:@"email"];
self.fName = [[NSUserDefaults standardUserDefaults]stringForKey:@"firstName"];
self.lName = [[NSUserDefaults standardUserDefaults]stringForKey:@"lastName"];
self.category = @"TestCategory";
return self;
}
@end
- (NSString *)email
{
return _email;
}
- (void)setEmail:(NSString *)email
{
_email = email;
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
[userDefault setObject:email forKey:@"email"];
}
我认为这是错误的,所以你可以做得更好,
- (void)actionMathodCalled
{
Card *card = [Card getCard];
NSLog(@"before saving to defaults->%@",card.email);
[card setEmail:@"happyCoding@ymail.com"];
NSLog(@"after savng to defaults->%@",card.email);
}
在您正在使用此共享实例的类中使用,如下所示
{{1}}
答案 3 :(得分:0)
+ (Card *)instance {
static Card *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[Card alloc] init];
});
return sharedInstance;
}
应该是工作