我有一个单例类,我打算在整个应用程序中共享它。它看起来像这样:
.m
@implementation PersonalGlobal
@synthesize firstName;
@synthesize lastName;
@synthesize SSN;
@synthesize customerNo;
@synthesize email;
@synthesize address;
@synthesize city;
@synthesize postalCode;
@synthesize telNo;
@synthesize mobileNo;
#pragma mark Singleton Methods
+ (id)sharedPersonal {
static PersonalGlobal *sharedPersonalGlobal = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedPersonalGlobal = [[self alloc] init];
});
return sharedPersonalGlobal;
}
- (void)dealloc {
[super dealloc];
// Should never be called
}
@end
。H
#import <foundation/Foundation.h>
@interface PersonalGlobal : NSObject {
NSString *firstName;
NSString *lastName;
NSString *SSN;
NSString *customerNo;
NSString *email;
NSString *address;
NSString *city;
NSString *postalCode;
NSString *telNo;
NSString *mobileNo;
}
@property (nonatomic, retain) NSString *firstName;
@property (nonatomic, retain) NSString *lastName;
@property (nonatomic, retain) NSString *SSN;
@property (nonatomic, retain) NSString *customerNo;
@property (nonatomic, retain) NSString *email;
@property (nonatomic, retain) NSString *address;
@property (nonatomic, retain) NSString *city;
@property (nonatomic, retain) NSString *postalCode;
@property (nonatomic, retain) NSString *telNo;
@property (nonatomic, retain) NSString *mobileNo;
+ (id)sharedPersonal;
@end
在代码中,我将字符串保存到它,如下所示:
PersonalGlobal *sharedPersonal = [PersonalGlobal sharedPersonal];
sharedPersonal.firstName = @"Some string";
但是当我改变视图并尝试访问这样的字符串时:
PersonalGlobal *sharedPersonal = [PersonalGlobal sharedPersonal];
//Setting some textfield
sometextfield.text = sharedPersonal.firstName;
我什么都得不到。我#import "PersonalGlobal.h"
在所有文件中都做了一个。我可以以任何方式将更改提交给单例类吗?
谁能看到我在这里做错了什么?谢谢!