WriteToFile: file read/write, writetofile read/write
About writeToFile read/write files: when the key-value pair in the Dictionary uses Model (for example, studentModel) as the value, it is found that the writeToFile method called by Dictionary cannot generate plist files. After multiple tests, it is found that, when the writeToFile method is called, the Dictionary cannot contain the model object. To solve this problem, the model data can be processed and converted to a Dictionary, which can be stored as a plist file. when reading the plist file, convert Dictionary to Model.
The following code uses studentModel as an example:
StudentModel has three attributes: name, passWord, and sex.
1 @property (nonatomic , copy)NSString *name;2 @property (nonatomic , copy)NSString *passWord;3 @property (nonatomic , copy)NSString *sex;
Add an instance method for studentModel to convert the Model object to a Dictionary
1 - (NSMutableDictionary *)changeModelToDic{2 NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];3 [dic setObject:self.name forKey:@"name"];4 [dic setObject:self.passWord forKey:@"passWord"];5 [dic setObject:self.sex forKey:@"sex"];6 return dic;7 }
Add class methods for studentModel. The Dictionary is converted to the Model object.
1 + (Student*)changDicToStudent:(NSMutableDictionary *)dic{2 Student *stu = [[Student alloc]init];3 stu.name = [dic objectForKey:@"name"];4 stu.passWord = [dic objectForKey:@"passWord"];5 stu.sex = [dic objectForKey:@"sex"];6 return stu;7 }