No it should not be a singleton. You should create your NSObject
-derived class like any other object:
MyCustomClass *myClass = [MyCustomClass new];
and then start populating it (via @property
accessors) from the JSON data (this assumes an array of dictionary objects, which is not uncommon):
for (unsigned i = 0; i < [jsonData count]; i++)
{
MyCustomClass *myClass = [MyCustomClass new];
NSDictionary *dict = [jsonData objectAtIndex:i];
myClass.name = [dict objectForValue:@"Name"];
myClass.age = [[dict objectForValue:"@"Age"] unsignedValue];
[someOtherArray addObject:myClass];
}
So your custom class can be as simple as:
@interface MyCustomClass : NSObject
@property (strong, nonatomic) NSString *name;
@property (assign, nonatomic) unsigned age;
@end
Of course things get interesting when holding more complex objects like dates, and you should use an NSDate
object to hold these and provide a string-to-date conversion method:
@interface MyCustomClass : NSObject
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSDate *dateOfBirth;
- (void)setDateOfBirthFromString:(NSString *)str;
@end
With the conversion method something like this:
- (void)setDateOfBirthFromString:(NSString *)str {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
self.dateOfBirth = [dateFormat dateFromString:str];
}