Словарь не сохраняется в файл - PullRequest
0 голосов
/ 03 ноября 2011

Я использую следующий код, чтобы сохранить NSMutableDictionary в файл, а затем получить его позже.

NSLog в методе сохранения работает нормально, но NSLog в методе получения возвращает ноль.

- (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;
}

NSLog self.workflowTemplate

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;
    };
}

Edit:

Обновлен словарь без 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 Ответ

4 голосов
/ 03 ноября 2011

Метод writeToFile:atomically: возвращает BOOL, который вы должны проверить. Если это не удается, то, скорее всего, в вашем словаре есть что-то, что не может быть записано. Только основные типы объектов могут быть записаны из словаря. См. Мой ответ на этот вопрос: iPhone - Запись словаря в файл: обработка ошибок , а также документы для writeToFile:atomically:.

...