WriteToFile: file read/write, writetofile read/write

Source: Internet
Author: User

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 }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.