Ios7 如何在iOS中创建NSObject类

Ios7 如何在iOS中创建NSObject类,ios7,nsobject,Ios7,Nsobject,我需要从web服务获取大量数据,它具有JSON格式。所以我创建了一个NSObject类来分配每个对象的属性。我想把JSON数据放到NSMutableArray中,然后使用for循环。在使用这些新的对象数组之后,我想填充UITableView ` for(int i=0;iNo)不应该是单例。您应该像其他任何对象一样创建NSObject派生类: MyCustomClass *myClass = [MyCustomClass new]; 然后开始(通过@property访问器)从JSON数据填充它

我需要从web服务获取大量数据,它具有
JSON
格式。所以我创建了一个
NSObject
类来分配每个对象的属性。我想把
JSON
数据放到
NSMutableArray
中,然后使用for循环。在使用这些新的对象数组之后,我想填充
UITableView

`


for(int i=0;iNo)不应该是单例。您应该像其他任何对象一样创建
NSObject
派生类:

MyCustomClass *myClass = [MyCustomClass new];
然后开始(通过
@property
访问器)从JSON数据填充它(这假设有一个字典对象数组,这并不少见):

当然,当保存更复杂的对象(如日期)时,事情会变得有趣,您应该使用
NSDate
对象来保存这些对象,并提供字符串到日期的转换方法:

@interface MyCustomClass : NSObject

@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSDate *dateOfBirth;

- (void)setDateOfBirthFromString:(NSString *)str;

@end
使用类似以下的转换方法:

- (void)setDateOfBirthFromString:(NSString *)str {
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd"];
    self.dateOfBirth = [dateFormat dateFromString:str]; 
}

不,它不应该是单例。您应该像创建任何其他对象一样创建
NSObject
派生类:

MyCustomClass *myClass = [MyCustomClass new];
然后开始(通过
@property
访问器)从JSON数据填充它(这假设有一个字典对象数组,这并不少见):

当然,当保存更复杂的对象(如日期)时,事情会变得有趣,您应该使用
NSDate
对象来保存这些对象,并提供字符串到日期的转换方法:

@interface MyCustomClass : NSObject

@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSDate *dateOfBirth;

- (void)setDateOfBirthFromString:(NSString *)str;

@end
使用类似以下的转换方法:

- (void)setDateOfBirthFromString:(NSString *)str {
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd"];
    self.dateOfBirth = [dateFormat dateFromString:str]; 
}