plist的一些方法

    // 首先读取studentInfo.plist中的数据
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"customInfo" ofType:@"plist"];
    NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    
    // 将学生信息填入视图
    NSDictionary *tmpInfo = [dictionary objectForKey: @"Student"];
    self.studentName.text = tmpInfo[@"Name"];
    self.studentGender.text = tmpInfo[@"Sex"];
    self.studentNum.text = tmpInfo[@"Num"];
    
    // 将导师信息写入视图
    self.mentorName.text = dictionary[@"Mentor"][@"Name"];
    self.mentorGender.text = dictionary[@"Mentor"][@"Gender"];
    
    // 修改plist
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    
    // 设置属性值,没有的数据就新建,已有的数据就修改
    data = @{@"Student":@{@"Name":@"lljj",
                          @"Sex":@"male",
                          @"Num":@"001"},
             @"Mentor":@{@"Name":@"Siri",
                         @"Sex":@"male"}};
    
    [data writeToFile:plistPath atomically:YES];
    
    NSDictionary *dictionary1 = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    
    // 将学生信息填入视图
    self.studentName.text = dictionary1[@"Student"][@"Name"];
    self.studentGender.text = dictionary1[@"Student"][@"Sex"];
    self.studentNum.text = dictionary1[@"Student"][@"Num"];
    
    // 将导师信息写入视图
    self.mentorName.text = dictionary1[@"Mentor"][@"Name"];
    self.mentorGender.text = dictionary1[@"Mentor"][@"Sex"];

你可能感兴趣的:(plist的一些方法)