ios - How to implement a class in Swift to create a global user for application? -


i'm making application has 1 mainuser class should accessible throughout application. example, mainuser class has variable fullname contains string full name of user. want able access variable on other scene of application maybe things like, "welcome fullname!"

how should go doing this? right now, making object of mainuser class in launch screen of application, , planning pass object along every following screen. doesn't seem efficient in mind, i'm wondering if there global method (although searched , found global variables rather unstable , unpopular.) application have 1 user, there 1 object ever created entire extent of app.

any ideas?

i have lot of experience object-oriented programming in java but, can tell question, i've grown rusty because i've been pretty inactive more year.

am not in swift hence posting code objective c. think can translate that. here main user class 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 singleton class storing 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 need call class every time

[appdefaults defaultinstance].currentuser.email = @"example@something.com"; 

and @ first when app opens need assign user defaults class

[appdefaults defaultinstance].currentuser = [[user alloc]init]; 

also if have data user can pass them , initialize user class , can assign user object defaults class. carry value throughout app. thing user data after app killed. need store users decoded object in nsuserdefaults retrieve next time app launches. here code goes

to store

user *user = [[user alloc]initwithdata:yourdatadictionary]; nsdata *myencodedobject = [nskeyedarchiver archiveddatawithrootobject:user]; [[nsuserdefaults standarduserdefaults] setobject:myencodedobject forkey:@"loggeduser"]; 

and retrieving , storing

nsdata *myencodedobject = [[nsuserdefaults standarduserdefaults] objectforkey:@"loggeduser" ]; user *obj = (user *)[nskeyedunarchiver unarchiveobjectwithdata: myencodedobject]; [appdefaults defaultinstance].currentuser = obj; 

sorry not translate in swift.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -