Am not so good in swift hence am posting the code with objective c. I think you can translate that. Here is the main user class which contains properties.
The .h file here
#import <Foundation/Foundation.h>
@interface User : NSObject
@property (nonatomic, strong) NSString *UserID;
@property (nonatomic, strong) NSString *Password;
@property (nonatomic, strong) NSString *UserName;
@property (nonatomic, strong) NSString *Email;
- (instancetype)initWithData:(NSDictionary*)dicData;
@end
The .m file here
#import "User.h"
@implementation User
@synthesize UserID;
@synthesize Password;
@synthesize UserName;
@synthesize Email;
- (instancetype)init
{
self = [super init];
if (self) {
UserID = @"";
Password = @"";
UserName = @"";
Email = @"";
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
//Encode properties, other class variables, etc
[encoder encodeObject:self.UserID forKey:@"UserID"];
[encoder encodeObject:self.Password forKey:@"Password"];
[encoder encodeObject:self.UserName forKey:@"UserName"];
[encoder encodeObject:self.Email forKey:@"Email"];
}
- (id)initWithCoder:(NSCoder *)decoder {
if((self = [super init])) {
//decode properties, other class vars
self.UserID = [decoder decodeObjectForKey:@"UserID"];
self.Password = [decoder decodeObjectForKey:@"Password"];
self.UserName = [decoder decodeObjectForKey:@"UserName"];
self.Email = [decoder decodeObjectForKey:@"Email"];
}
return self;
}
- (instancetype)initWithData:(NSDictionary*)dicData
{
self = [super init];
if (self) {
UserID = [dicData valueForKey:@"userid"];
Password = [dicData valueForKey:@"password"];
UserName = [dicData valueForKey:@"username"];
Email = [dicData valueForKey:@"email"];
}
return self;
}
- (void)dealloc
{
UserID= nil;
Password = nil;
UserName= nil;
Email= nil;
}
@end
And your Singleton Class for storing this class object goes follows.
the .h file here
#import <Foundation/Foundation.h>
#import "User.h"
@interface AppDefaults : NSObject
@property (nonatomic, strong) User *currentUser;
+ (AppDefaults *)defaultInstance;
@end
and .m file here
#import AppDefaults.h
@implementation AppDefaults
@synthesize currentUser;
static AppDefaults *staticInstance;
+ (AppDefaults *)defaultInstance{
static dispatch_once_t once;
dispatch_once(&once, ^{
staticInstance=[[self alloc] init];
});
return staticInstance;
}
- (instancetype)init
{
self = [super init];
if (self) {
currentUser = [[User alloc]init];
}
return self;
}
@end
Now you need to call the class like this every time
[AppDefaults defaultInstance].currentUser.Email = @"example@something.com";
and at first when app opens you need to assign the user to the defaults class
[AppDefaults defaultInstance].currentUser = [[User alloc]init];
also if you have data for the user then you can pass them and initialize the user class and then you can assign the user object to the defaults class.
This will carry value throughout the app. Only thing is to get the user data even after app killed. You need to store users decoded object in NSUserDefaults to retrieve it next time the app launches. Here the code goes
to store
User *user = [[User alloc]initWithData:yourDataDictionary];
NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:user];
[[NSUserDefaults standardUserDefaults] setObject:myEncodedObject forKey:@"loggedUser"];
and for retrieving and storing
NSData *myEncodedObject = [[NSUserDefaults standardUserDefaults] objectForKey:@"loggedUser" ];
User *obj = (User *)[NSKeyedUnarchiver unarchiveObjectWithData: myEncodedObject];
[AppDefaults defaultInstance].currentUser = obj;
Sorry i could not translate it in swift.