将NSString和NSNumber附加到NSMutableArray并生成NSMutabledictionary

时间:2013-08-12 06:31:22

标签: ios objective-c cocoa-touch

我从一个类中的以下变量中获取SQL服务器中的数据:

@property(retain,nonatomic) NSString* trainingName;
@property(retain,nonatomic) NSNumber* trainingCount;

在其他课程中,我想在NSMutableArray中追加这个值,最后制作一个dictonary.I我这样做:

-(void)getTrainingDetails
{
MyTestSp_GetTrainingCountsList *objReturnTrainings = [MyTestSp_GetTrainingCounts findAll];
NSMutableArray *trainingNames = [[NSMutableArray alloc]init];
NSMutableArray *trainingCounts = [[NSMutableArray alloc]init];
NSMutableDictionary *dictTrainings = [[NSMutableDictionary alloc]init];

if ([objReturnTrainings length] > 0)
{
    for (MyTestSp_GetTrainingCounts *obj in objReturnTrainings)
    {
        // get the values and assign to NSMutable array
        trainingNames = trainingName;
        trainingCounts = trainingCount;
        //Make the dictionary.
    }

}

}

这是正确的做法吗?如何将其放入字典?

请帮忙。

1 个答案:

答案 0 :(得分:2)

您似乎正在尝试将数组直接设置为您的数字/字符串(您实际上没有从obj中获取,因此您的代码不应该编译...)。您也不需要数组来创建字典。你可以这样做:

for (MyTestSp_GetTrainingCounts *obj in objReturnTrainings)
{
    dictTrainings[obj.trainingName] = obj.trainingCount;
}