---
title: "怎么从沙盒里提取数据"
author: "张亚雄"
date: "2015-07-21T11:15:50Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2015-07-23T09:26:18Z"
id: 2228987
url: "https://792783226.iteye.com/blog/2228987"
---

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