灵活的字典

时间:2014-10-14 09:38:15

标签: ios objective-c nsdictionary

我正在尝试创建一个基于xml文件的灵活字典。我使用xmlDigester和一些规则,一切顺利,直到我试图解析我的字典的值。

上面是我的对象类,它使用XMLdigister创建Dictionary:

@interface slideInfo : NSObject
@property (nonatomic, strong) NSNumber *id;
@property (nonatomic, strong) NSString *appname;
@property (nonatomic, strong) NSString *marketinfos;
@property (nonatomic, strong) NSString *questionMarket;
@property (nonatomic, strong) NSString *theText;

@property (nonatomic, strong) NSMutableDictionary * mrtInfos;

-(void)addInfo:(marketInfos*)market;
-(void)questionMarket:(answerHandler*)qMarket;

@end

.m文件

@implementation slideInfo

@synthesize id, version, appname, marketinfos, questionMarket, mrtInfos, theText;

- (id)init {

    if ((self = [super init])) {
        self.mrtInfos = [NSMutableDictionary new];
    }
    return self;
}

-(void)addInfo:(marketInfos*)market
{
    NSString *key = [NSString stringWithFormat:@"%@ %@", market.name, market.image];
    [mrtInfos setObject:market forKey:key];

}

-(void)questionMarket:(answerHandler*)qMarket
{
    NSString *key = [NSString stringWithFormat:@"%@ %@ %@ %@", qMarket.name, qMarket.image, qMarket.question, qMarket.answer];
    [mrtInfos setObject:qMarket forKey:key];
    theText = qMarket.question;
    NSLog(@"Dic: %@" , theText);
}


@end

answeHandler.h

@interface answerHandler : NSObject

@property (nonatomic, strong) NSString *id;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *image;
@property (nonatomic, strong) NSString *question;
@property (nonatomic, strong) NSString *answer;

-(NSString *)marketString;

@end

和.m

@implementation answerHandler

@synthesize id, name, image, question, answer;

- (id)init {
    if ((self = [super init])) {
        NSLog(@"answerHandler init");
    }
    return self;
}

-(NSString *)marketString {
    return [NSString stringWithFormat:@"%@ %@ %@ %@ %@" ,id, name, image, question, answer];
}

@end

现在我的问题是如何从字典中检索特定值?我试过的一切都返回null。提前谢谢

1 个答案:

答案 0 :(得分:0)

主要问题是您必须在字典上检索/搜索哪些数据。为什么你不使用id对象作为NSDictionary中的密钥?

-(void)addInfo:(marketInfos*)market
{
    NSString *key = [NSString stringWithFormat:@"%@ %@", market.id];
    [mrtInfos setObject:market forKey:key];

}

然后使用该ID在您的字典中搜索;

-(marketInfos*)objectFromDictionary:(NSNumber*)id
{

    return =  [mrtInfos objectForKey:id];

}