データを保存して取得します。
IOS開発において、データ保存の問題がよくあります。今は主にwriteでデータを保存し、必要な時にデータを取得し、write to fileを使ってデータを永久にディスクに保存する方法を説明します。
NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path=[paths objectAtIndex:0];
NSLog(@"path = %@",path);
NSString *FileName=[documentDirectory stringByAppendingPathComponent:@"test.plist"];//test.plist
NSDictionary *dic = [NSDictionarydictionaryWithObjectsAndKeys:label2.text,@"1",label4.text,@"2",label6.text,@"3", label7.text,@"4", nil];// label.text value ,
[dic writeToFile:filename atomically:YES];
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path=[paths objectAtIndex:0];//
NSLog(@" %@",path);
NSString *file=[path stringByAppendingPathComponent:@"test.plist"];//
dic2 = [NSDictionary dictionaryWithContentsOfFile:file];//
NSLog(@" %@",dic2);
これは異なるcontrollerの間でデータを取得し、同じcontrollerでデータを取得するなら、取得経路を直接self.pathで利用しなくても良い。