如何编写辅助函数来读取/写入我的NSDictionary到我的属性列表?

时间:2013-12-08 07:06:57

标签: ios iphone objective-c

我正在开发一个iPhone应用程序,允许从属性列表中读取和保存某些数据,但我的读取功能无法正常工作。如果我的想法是正确的,我应该能够简单地反转我为阅读编写写辅助函数所采用的过程。我能够成功使用[NSArray * dic valueForKey],但我的NSLog语句没有打印任何内容。程序没有崩溃,但它也没有在输出窗口中显示记录的值。我不确定我是否正确找到了我的filePath,创建了字典或数组。我知道我必须在主视图控制器中调用我的init函数,但是我很难这样做。

数据头中的函数声明如下:

    @property (nonatomic, copy) NSMutableArray *listOfGroups;

    + (AllData*) myGroupList;

    -(NSString*) plistFilePath;

    -(NSMutableArray*) withNSMutableArrayInit: (NSMutableArray*) arr;

我在数据实现中的功能如下:

    -(NSString *) plistFilePath
    {
        //---1.Find the property list filePath
        NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString* documentsDir = [paths objectAtIndex:0];
        NSString* path = [documentsDir stringByAppendingPathComponent:@"Group.plist"];
        return documentsDir;


        //---2. Create a dictionary and initialize with contents of file.---
        NSDictionary *dictionary = [[NSMutableDictionary alloc]initWithContentsOfFile:documentsDir];


        //---3. Create an array of type arrayWithArray:[dictionary allValues].---
        NSArray* array= [NSArray arrayWithArray:dictionary];
        return [self plistFilePath];
    }

    - (NSMutableArray*) WithNSMutableArrayInit: (NSMutableArray*) arr
    {
    [self plistFilePath];

    for (NSDictionary *dic in arr)
    {
        NSString *groupName = [dic valueForKey:@"groupName"];
        NSLog(groupName);
        NSString *createDate = [dic valueForKey:@"createDate"];
        NSString *totalAmount = [dic valueForKey:@"totalAmount"];
        NSString *totalPeople = [dic valueForKey:@"totalPeople"];
        NSArray* participantList = [dic valueForKey:@"ParticipantList"];

        for (NSDictionary *dic in arr)
        {
            NSString *participantName = [dic valueForKey:@"participantName"];
            NSLog(participantName);
            NSString *relatedParticipantList = [dic valueForKey:@"relatedParticipantList"];
            NSString *relatedAmountList = [dic valueForKey:@"relatedAmountList"];
        }

        NSArray* recordList = [dic valueForKey:@"recordList"];
        for (NSDictionary *dic in arr)
        {
            NSString *recordCategory = [dic valueForKey:@"recordCategory"];
            NSLog(recordCategory);
            NSString *recordDateTime = [dic valueForKey:@"recordDateTime"];
            NSString *recordAmount = [dic valueForKey:@"recordAmount"];
        }

    }

return [self listOfGroups];
    }

1 个答案:

答案 0 :(得分:1)

您的plistFilePath方法提前返回用户文档路径,而不是文件路径。你的意思是这样吗?它看起来像一个bug。

更不用说如果您通过在最终(当前无法访问)返回语句中调用[self plistFilePath]来修复该错误,则会有无限循环。