Reputation: 5545
Need to create MODEL for following. I have below Json Response from web service response,
Now i want to create its model so we can easily fetch it something like
EmployeeClass.status =
EmployeeClass.statusCode =
EmployeeClass.description =
EmployeeClass.employeeId =
EmployeeClass.Present =
EmployeeClass.Leave =
EmployeeClass.Absent =
EmployeeClass.PFCode=
My JSON string is
{
"EmployeeClass": {
"responseStatus": {
"status": "SUCCESS",
"statusCode": "100",
"description": "Success",
"employeeId": "1011"
},
"Present": 22,
"Leave": 1,
"Absent": 4
},
"PFCode": "ABC123"
}
What i tried so far
Create singleton class EmployeeClass
//------.h class----------------
#import < Foundation/Foundation.h >
@interface EmployeeClass : NSObject
@property (nonatomic, strong)NSString * status ;
@property (nonatomic, strong)NSString * statusCode ;
@property (nonatomic, strong)NSString * description ;
@property (nonatomic, strong)NSString * employeeId ;
@property (nonatomic, strong)NSString * Present ;
@property (nonatomic, strong)NSString * Leave ;
@property (nonatomic, strong)NSString * Absent ;
@property (nonatomic, strong)NSString * PFCode;
+ (EmployeeClass*)sharedSingleton;
@end
//----------.m class-----------
#import "EmployeeClass.h"
@implementation EmployeeClass
- (id)init {
self = [super init];
if(self) {
self.status = [NSString new];
self.statusCode = [NSString new];
self.description = [NSString new];
self.employeeId = [NSString new];
self.Present = [NSString new];
self.Leave = [NSString new];
self.Absent = [NSString new];
self.PFCode = [NSString new];
//do any more customization to your location manager
}
return self;
}
+ (EmployeeClass *)sharedSingleton {
static EmployeeClass *sharedSingleton;
@synchronized(self) {
if (sharedSingleton == nil) {
sharedSingleton = [[EmployeeClass alloc] init];
}
}
return sharedSingleton;
}
@end
Upvotes: 0
Views: 80
Reputation: 401
I don't know if I understood your question correctly, but you shouldn't do something like EmployeeClass.property =
because you are trying to access some property stored in a CLASS, and that doesn't make sense. You have to access an Instance of that class.
So first you have to retrieve the instance of the singleton: EmployeeClass *obj = [EmployeeClass sharedSingleton];
and then you can access the properties of the object: obj.status =
To parse a JSON string you can do something like this:
NSError *error;
NSData *jsondata = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary respObj = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
So now you have a Dictionary with all the properties. To access the single properties: [respObj valueForKey:@"anyKey"];
. For Example obj.status = [respObj valueForKey:@"EmployeeClass"] valueForKey:@"responseStatus"] valueForKey:@"status"]
. This because the first and the second "valueForKey" message will return another NSDictionary.
Hope this helps.
Upvotes: 1
Reputation: 155
Why create a singleton ?. if you want several employe object. You can't have only one instance of employe. You can check if employeeId is already taken and create a new Employe instead.
For example :
Employe *employe = [self getEmployeWithId:employeId];
if (!employe){
employe = [employe MR_createInContext:yourContext];
employe.employeId = yourNewId;
}
Upvotes: 0