0

我有类人。

人.h

#import <Foundation/Foundation.h>

@interface Person : NSObject{
    NSString *name;
    NSString *screen_name;
}
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *screen_name;

@end

人.m

#import "Person.h"

@implementation Person

@synthesize text,screen_name;

@end

我有 NSArray 对象,带有值,例如文本、屏幕名称等。

我愿意:

Person * person = object; ( object is NSArray)

我想得到值,像这样:人。姓名,人。屏幕名称

对象

{
        "screen_name" = "mika_alcala";
        "text" ="";
        "time_zone" = "Eastern Time (US & Canada)";
        "url" = "<null>";
        "utc_offset" = "-14400";
        "verified" = 0;
};
4

1 回答 1

1

好的,首先您不需要定义 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

有两种方法可以做到这一点......

  1. 创建一个空白Person对象,然后设置字典中的值。

    Person *person = [[Person alloc] init];
    person.name = object[@"screen_name"];
    person.screen_name = object[@"screen_name"];
    
  2. 向对象添加自定义 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.nameperson.screen_name

于 2013-07-25T16:47:17.843 回答