好的,首先您不需要定义 ivars,也不需要使用 XCode 4.5+ (IIRC) 进行综合。
所以它应该看起来像......
人.h
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *screen_name;
@end
人.m
#import "Person.h"
@implementation Person
@end
对于实际问题,我不确定您在问什么。我得再问...
编辑以显示如何从数组创建
首先开...
object :
{
"screen_name" = "mika_alcala";
"text" ="";
"time_zone" = "Eastern Time (US & Canada)";
"url" = "<null>";
"utc_offset" = "-14400";
"verified" = 0;
};
这个对象实际上是一个NSDictionary
非和NSArray
。
有两种方法可以做到这一点......
创建一个空白Person
对象,然后设置字典中的值。
Person *person = [[Person alloc] init];
person.name = object[@"screen_name"];
person.screen_name = object[@"screen_name"];
向对象添加自定义 init 方法Person
。
人.h
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *screen_name;
- (id)initWithDictionary:(NSDictionary *)dictionary;
@end
人.m
#import "Person.h"
@implementation Person
- (id)initWithDictionary:(NSDictionary *)dictionary
{
self = [super init];
if (self) {
_name = dictionary[@"screen_name"];
_screen_name = dictionary[@"screen_name"];
}
return self;
}
@end
然后你可以做...
Person *person = [[Person alloc] initWithDictionary:object];
无论您采用哪种方法,您都将能够掌握person.name
和person.screen_name
。