阅读嵌套的NSMutableDictionaries的内容?

时间:2012-03-14 01:27:17

标签: objective-c loops for-loop nsarray nsmutabledictionary

看起来很简单 - 我正在创建一个主NSMutableDic并填充更多它们。

访问像这样的字典内容的简单方法是什么? 虽然这不是Python,也许是'taskDict [4] ['assignedby']'的程度?

这是代码,工作正常和花花公子:

int counter = 0;

NSArray *cols = [GetProjectInfo getTaskColumns];    
NSArray *rows = [GetProjectInfo fetchAll:projectName];

NSMutableDictionary *taskDict = [NSMutableDictionary dictionary];
NSMutableDictionary *localDict = [NSMutableDictionary dictionary];

for (int x=0; x < rows.count; x++) {

    NSString *rowData = [rows objectAtIndex:x];

    NSString *colData = [cols objectAtIndex:counter];

    //Assign the values colData(key) and rowData(value) into localDict.
    [localDict setValue:rowData forKey:colData];

    counter ++;

    if (counter >= cols.count) {

        counter = 0;

        //Add the whole localDict to the global dictionary (taskDict)
        [taskDict setObject:localDict forKey:localDict];

        //Reset the localDict so that we can populate it with the next set 
        [localDict removeAllObjects];
    }
}

NSLog(@"%@",taskDict);

返回的内容为:

            {
    person = "Ryan";
    assignedby = jfreund;
    complete = False;
    timeassigned = "15:35:00";
} =     {
};
    {
    person = "Tim";
    assignedby = klang;
    complete = True;
    timeassigned = "16:59:07";
} =     {
};

有人在意这个方向吗?

1 个答案:

答案 0 :(得分:0)

这不能直接回答你的问题;但是,您是否可以使用NSObject子类来封装数据,而不是将对象图形存储为嵌套字典,而不是将对象图形存储为嵌套字典?例如,(请注意,我对您的应用程序的模型层一无所知......)您可以将概念模型分解为Project和Task对象,例如。

@interface Task : NSObject <NSCoding>
{
    NSString *_name;
    NSString *_owner;
    NSString *_assignedBy;
    BOOL _complete;
    NSDate *_timeAssigned;
    //  etc.
}

然后,Projects又由任务组成:

@interface Project : NSObject <NSCoding>
{
    NSString *_name;
    NSString *_dateStarted;
    NSMutableArray *_tasks;
    //  etc.
}

这是一种面向对象的构建应用程序模型层的方法。只要您的类符合NSCoding协议,这种机制就不会阻止您将数据序列化为plist。