本地数据的写入(保存)和读取 plist

首先在AppDelegate.m中 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中初始话本地文件: NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths objectAtIndex:0];
    NSLog(@"%@",path);
    self.filename=[path stringByAppendingPathComponent:@"test.plist"];
//test.plitst文件创建;


AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSMutableArray *temp=[NSKeyedUnarchiver unarchiveObjectWithFile:appDelegate.filename];//先用数组读取本地数据

if (temp == nil) {
        temp = [[NSMutableArray alloc]initWithCapacity:0];//
    }
   
    NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:m_labelpeople.text,@"1",m_labelrestaurant.text,@"2",m_labelpackage.text,@"3",price,@"4", nil];
    [temp addObject:dic];//在数组中继续添加界面中label的值
[NSKeyedArchiver archiveRootObject:temp toFile:appDelegate.filename];//将数组中值保存到本地


self.listdata = [NSKeyedUnarchiver unarchiveObjectWithFile:appDelegate.filename];//用listdata数组读取本地数据
   

猜你喜欢

转载自cydd.iteye.com/blog/2147916
今日推荐