键值编码数组问题

时间:2011-07-11 09:51:39

标签: iphone objective-c ipad restkit

我有以下课程:

@interface Topics : NSObject {
    NSNumber * _until_id;
    NSArray * _topics;
}

@property (nonatomic, retain) NSNumber * until_id;
@property (nonatomic, retain) NSArray * topics;
@end

/////////////////////////////////////////////////////////////////////////

@class Login;

@interface Topic : NSObject {
    NSString * _name;
    Login * _creator;
    NSNumber * _message_count;
    NSNumber * _date_latest_message;
    NSNumber * _date_created;
    NSNumber * _tracked;
    NSNumber * _unread;
    NSNumber * _tid;
    NSString * _kind;
    NSNumber * _id;
}

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) Login * creator;
@property (nonatomic, retain) NSNumber * message_count;
@property (nonatomic, retain) NSNumber * date_latest_message;
@property (nonatomic, retain) NSNumber * date_created;
@property (nonatomic, retain) NSNumber * tracked;
@property (nonatomic, retain) NSString * kind;
@property (nonatomic, retain) NSNumber * unread;
@property (nonatomic, retain) NSNumber * tid;
@property (nonatomic, retain) NSNumber * gid;
@property (nonatomic, readonly) NSString* topicNavURL;

@end

现在我正尝试使用以下方式访问特定主题名称:

RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[TTTableTextItem class]];
 [mapping mapKeyPath:@"topics.name" toAttribute:@"text"];
 [mapping mapKeyPath:@"topics.topicNavURL" toAttribute:@"URL"];

然而,这失败了,因为它说找不到名称,因为主题是NSArray。如果我正在处理数组,有没有办法做KVC?那只是NSString吗?

1 个答案:

答案 0 :(得分:0)

是的,您无法使用KVC直接访问阵列。 但是,您可以使用@sum,@ avg等聚合来计算总和,平均值

例如:@ sum.message_count将为您提供数组中所有消息的总消息数。您不必为此编写任何循环。