本地数据的写入(保存)和读取 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数组读取本地数据
   

你可能感兴趣的:(Objective-C,iOS,Cocoa)