Reputation: 7
i have to show the user details from NSUserDefaults
in more than 5 view controllers. So i have created a NSObject
subclass, which will load the user details from server when the first view controllers viewDidLoad
is called.
Here is my First view controller viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// Getting the Current User Details
CurrentUserDetails *userDetails = [[CurrentUserDetails alloc]init];
[userDetails initializeTheCurrentUserData];
//CurrentUserDetails is my NSObject class
}
And
#import <Foundation/Foundation.h>
@interface CurrentUserDetails : NSObject
@property(strong,nonatomic) NSString *memberName;
@property(strong,nonatomic) NSString *designation;
@property(strong,nonatomic) NSString *memberType;
@property(strong,nonatomic) NSString *entreprenuer;
@property(strong,nonatomic) NSDate *expiryDate;
@property(strong,nonatomic) NSData *imageData;
- (void) initializeTheCurrentUserData;
@end
and implementation
@implementation CurrentUserDetails
- (void) initializeTheCurrentUserData{
NSData *data = [[NSUserDefaults standardUserDefaults] valueForKey:@"userDictionary"];
NSDictionary *retrievedDictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data];
self.memberName = [retrievedDictionary valueForKey:@"Name"];
self.designation = [retrievedDictionary valueForKey:@"Designation"];
self.memberType = [[retrievedDictionary valueForKey:@"Member_type"] stringValue];
self.expiryDate = [retrievedDictionary valueForKey:@"Expiry"];
self.kanaraEntreprenuer = [retrievedDictionary valueForKey:@"CityName"];
NSString *imageUrl = [retrievedDictionary valueForKey:@"Member_image"];
self.imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[GlobalVariables getBaseURLForMemberImage],imageUrl]]];
}
And when iam trying to take the details from other class like this..
CurrentUserDetails *userDetails = [[CurrentUserDetails alloc]init];
memberName = userDetails.memberName;
designation = userDetails.designation;
memberType = userDetails.memberType;
dateFromServer = userDetails.expiryDate;
entreprenuer = userDetails.entreprenuer;
imageDataFromServer = userDetails.imageData;
I am getting nil values.
But if call initializeTheCurrentUserData method each time, i am getting the exact values. I though once a property is assigned with a value , we can use the property for entire program. I'm getting confusion.. Can anyone please tell me about this????. Do i need to call initializeTheCurrentUserData everytime when i want to use the values?
Upvotes: 0
Views: 54
Reputation: 42588
Once you set a property of an instance, that property remains for that instance. You, however, are creating new instances with [[CurrentUserDetails alloc] init]
. Each new instance will be initialized with default values (nil
for NSString
).
Call -initializeTheCurrentUserData
in -init
so each instance will be initialized with the values from user defaults.
@implementation CurrentUserDetails
- (instancetype)init {
self = [super init];
if (self != nil) {
[self initializeTheCurrentUserData];
}
return self;
}
- (void)initializeTheCurrentUserData {
…
}
Upvotes: 2