怎么从沙盒里提取数据

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        //获取沙盒中Documents文件的路径(注意我们是给它写了一个地址而这个地址是空的没有房子没有车,也没有钱,就像只是土地规划中土地局那帮孙子们给发了一个土地证一样。)
NSString *str_file_path = [paths objectAtIndex:0];
//所引用数据个数(一般都是1个文件所以用X-1=0,基本上算是固定的吧,别过分这里留时间去分析它。)
NSString *str_data_file_path = [str_file_path stringByAppendingPathComponent:@"order.plist"];
        //将自己想创建的文件名添加到Documents录后,拼成一整个字符串(就是准备盖房子之前这个房子要买多少砖,多少水泥,多少钢筋,打多深的柱脚。@“order.plist"这里面是你要存进沙盒的文件名称)
如果数据很多建议用字典编好内容再逐一的放入沙盒。举个例子:
    NSDictionary *dicKFC1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"超值多人餐",@"name",@"¥20",@"price" ,nil];
    NSDictionary *dicKFC2= [[NSDictionary alloc]initWithObjectsAndKeys:@"美味汉堡",@"name",@"¥11",@"price", nil];
    NSArray *arrKFC = [[NSArray alloc]initWithObjects:dicKFC1,dicKFC2,nil];


   
 
    NSDictionary *dicMDL1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"脆香油条",@"name",@"¥10",@"price", nil];
    NSDictionary *dicMDL2 =[[NSDictionary alloc]initWithObjectsAndKeys:@"圣代",@"name",@"¥8",@"price",nil];
    NSArray *arrMDL = [[NSArray alloc]initWithObjects:dicMDL1,dicMDL2 ,nil];
m_dicComboData =[[NSDictionary alloc]initWithObjectsAndKeys:arrKFC,@"KFC",arrMDL,@"MDL",arrXbc,@"星巴克",nil];

-(void)showAlertView: (id)sender
{
    if(![m_nameLabel.text isEqualToString: @""] && ![m_resrveLabel.text  isEqualToString: @""] && ![m_cobmoLabel.text  isEqualToString: @""]&&![N_nameLabel.text isEqualToString:@""])
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        //获取沙盒中Documents文件的路径
        NSString *str_file_path = [paths objectAtIndex:0];
        //将自己想创建的文件名添加到Documents录后,拼成一整个字符串
        NSString *str_data_file_path = [str_file_path stringByAppendingPathComponent:@"order.plist"];
        //        初始化字典
        NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:m_nameLabel.text,@"name",m_resrveLabel.text,@"restaurant",m_cobmoLabel.text,@"combo",m_strprice,@"price",N_nameLabel.text,@"noName",nil];
       
        //     zhi xing zhe lu jing
        NSMutableArray *array_order = [[NSMutableArray alloc]initWithContentsOfFile:str_data_file_path];
        if (array_order == nil)
        {
            array_order = [[NSMutableArray alloc]initWithCapacity:0];
        }
   
        [array_order addObject:dic];
        BOOL is_save_success = [array_order writeToFile:str_data_file_path atomically:YES];
        if (is_save_success == true)
        {
            [(UIButton *)sender setEnabled:NO];
           
        }
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示信息" message:@"用户输入不完整,订餐失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
   
}


猜你喜欢

转载自792783226.iteye.com/blog/2228987
今日推荐