字典未保存到文件

时间:2011-11-03 18:03:24

标签: iphone objective-c cocoa-touch nsdictionary sandbox

我正在使用以下代码将NSMutableDictionary保存到文件,然后再检索它。

save方法中的NSLog工作正常,但retreive方法中的NSLog记录为null。

- (void)saveDataToFile
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"cahchedTemplates"];
    [self.workflowTemplates writeToFile:filePath atomically:TRUE];
    NSLog(@"template save: %@", self.workflowTemplates);
}

- (IBAction)retrieveDataButtonPressed:(id)sender
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"cahchedTemplates"];
    NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithContentsOfFile:filePath]; 
    NSLog(@"tempDict: %@", tempDic);
}

+(NSMutableDictionary*)getClinicalTemplatesAndWorkFlowTemplates:(NSInteger)appointmentId
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/ichrono/20110715/60b88126/fetch_clinical_templates/", [self getDrChronoHost]]];

    ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
    [self addCurrentUserLoginToPostRequest:request];
    [request setPostValue:[NSString stringWithFormat:@"%d", appointmentId] forKey:@"appointment_id"];

    [request startSynchronous];
    NSError *error = [request error];
    NSString *responseString;
    if (!error) {
        responseString = [request responseString];
    } else {
        //return [[NSString alloc] initWithString:@"Failed to connect to DrChrono.com, please check your WiFi or 3G connection."];
        NSMutableDictionary *returnContainer = [[[NSMutableDictionary alloc] init] autorelease];
        [returnContainer setObject:[[[NSArray alloc] init] autorelease] forKey:@"custom_templates"];
        [returnContainer setObject:[[[NSMutableDictionary alloc] init] autorelease] forKey:@"workflow_templates"];
        return returnContainer;
    }

    NSDictionary *responseDictionary = [responseString JSONValue];
    if ((NULL == responseDictionary) || (NULL == [responseDictionary valueForKey:@"errors"])) {
        NSMutableDictionary *returnContainer = [[[NSMutableDictionary alloc] init] autorelease];
        [returnContainer setObject:[[[NSArray alloc] init] autorelease] forKey:@"custom_templates"];
        [returnContainer setObject:[[[NSMutableDictionary alloc] init] autorelease] forKey:@"workflow_templates"];
        return returnContainer;
    }

    NSMutableArray *templates = [[NSMutableArray alloc] init];
    for (id it in [responseDictionary valueForKey:@"custom_templates"]) {
        NSMutableDictionary *clinical_template = [[[NSMutableDictionary alloc] init] autorelease];

        [clinical_template setValue:[it valueForKey:@"has_data_flag"] forKey:@"has_data_flag"];

        if (0) {
            //@note: mnusimow 20101010: Activate this code to put a "*" character at beginning of templates with values.
            if (0 != [[it valueForKey:@"has_data_flag"] intValue]) {
                [clinical_template setValue:[NSString stringWithFormat:@"*%@", [it valueForKey:@"name"]] forKey:@"display"];
            } else {
                [clinical_template setValue:[it valueForKey:@"name"] forKey:@"display"];
            }
        } else {
            [clinical_template setValue:[it valueForKey:@"name"] forKey:@"display"];
        }
        [clinical_template setValue:[it valueForKey:@"id"] forKey:@"template_id"];

        [templates addObject:clinical_template];
    }

    NSMutableDictionary *returnContainer = [[[NSMutableDictionary alloc] init] autorelease];
    [returnContainer setObject:templates forKey:@"custom_templates"];
    [returnContainer setObject:[responseDictionary valueForKey:@"workflow_templates"] forKey:@"workflow_templates"];
    return returnContainer;
}

self.workflowTemplate的NSLog

2011-11-03 13:12:11.178 app[2869:1a603] sdfsafsafa:{
    "has_manual_edits_flag" = 0;
    "hp_assessment" =     {
        "has_data_flag" = 0;
        "has_manual_edits_flag" = 0;
        id = 77607;
    };
    "hp_cc_history_present_illness" =     {
        "has_data_flag" = 0;
        "has_manual_edits_flag" = 0;
        id = 77603;
    };
    "hp_med_fam_social_history" =     {
        "has_data_flag" = 0;
        "has_manual_edits_flag" = 0;
        id = 77604;
    };
    "hp_medications_allergies" =     {
        "has_data_flag" = 0;
        "has_manual_edits_flag" = 0;
        id = "<null>";
    };
    "hp_physical_exam" =     {
        "has_data_flag" = 0;
        "has_manual_edits_flag" = 0;
        id = 77606;
    };
    "hp_plan" =     {
        "has_data_flag" = 0;
        "has_manual_edits_flag" = 0;
        id = 77608;
    };
    "hp_review_of_systems" =     {
        "has_data_flag" = 0;
        "has_manual_edits_flag" = 0;
        id = 77605;
    };
    "soap_assessment" =     {
        "has_data_flag" = 0;
        "has_manual_edits_flag" = 0;
        id = 77611;
    };
    "soap_objective" =     {
        "has_data_flag" = 0;
        "has_manual_edits_flag" = 0;
        id = 77610;
    };
    "soap_plan" =     {
        "has_data_flag" = 0;
        "has_manual_edits_flag" = 0;
        id = 77612;
    };
    "soap_subjective" =     {
        "has_data_flag" = 0;
        "has_manual_edits_flag" = 0;
        id = 77609;
    };
}

编辑:

更新了没有id object

的词典
2011-11-03 16:28:31.136 app[11158:1a603] sdfsafsafa:{
    "has_manual_edits_flag" = 0;
    "hp_assessment" =     {
        "db_id" = 77607;
        "has_data_flag" = 0;
        "has_manual_edits_flag" = 0;
    };
    "hp_cc_history_present_illness" =     {
        "db_id" = 77603;
        "has_data_flag" = 1;
        "has_manual_edits_flag" = 0;
    };
    "hp_med_fam_social_history" =     {
        "db_id" = 77604;
        "has_data_flag" = 0;
        "has_manual_edits_flag" = 0;
    };
    "hp_medications_allergies" =     {
        "db_id" = "<null>";
        "has_data_flag" = 0;
        "has_manual_edits_flag" = 0;
    };
    "hp_physical_exam" =     {
        "db_id" = 77606;
        "has_data_flag" = 0;
        "has_manual_edits_flag" = 0;
    };
    "hp_plan" =     {
        "db_id" = 77608;
        "has_data_flag" = 0;
        "has_manual_edits_flag" = 0;
    };
    "hp_review_of_systems" =     {
        "db_id" = 77605;
        "has_data_flag" = 0;
        "has_manual_edits_flag" = 0;
    };
    "soap_assessment" =     {
        "db_id" = 77611;
        "has_data_flag" = 0;
        "has_manual_edits_flag" = 0;
    };
    "soap_objective" =     {
        "db_id" = 77610;
        "has_data_flag" = 0;
        "has_manual_edits_flag" = 0;
    };
    "soap_plan" =     {
        "db_id" = 77612;
        "has_data_flag" = 0;
        "has_manual_edits_flag" = 0;
    };
    "soap_subjective" =     {
        "db_id" = 77609;
        "has_data_flag" = 0;
        "has_manual_edits_flag" = 0;
    };
}

1 个答案:

答案 0 :(得分:4)

方法writeToFile:atomically:会返回BOOL,您应该检查。如果它失败了,那么你很可能在你的词典中有一些无法写出的东西。只能从字典中写出基本对象类型。请参阅我对此问题的回答:iPhone - Writing a dictionary to file : handling errors以及writeToFile:atomically:的文档。