我将两个 CGPoints(在一个数组中)保存到 Documents 目录中的一个文本文件中。当我阅读文件时,这是我得到的:
NSPoint: {66.5, 87}, NSPoint: {198.5, 314.5}
如何获得这样的输出:
66.5 87
198.5 314.5
这是我的代码:
//save the coordinates of two points into a text file in the Documents directory
- (void)savePoints
{
//get Document directory's path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"POINTS.txt"];
//create an points array
NSMutableArray *points = [[NSMutableArray alloc] init];
[points addObject:[NSValue valueWithBytes:&fromPoint objCType:@encode(CGPoint)]]; //insert object at the end of the array
[points addObject:[NSValue valueWithBytes:&toPoint objCType:@encode(CGPoint)]];
//write points array to file
BOOL result = [NSKeyedArchiver archiveRootObject:points toFile:fullPath];
NSLog(@"Archival result: %d", result);
}
//read the file
-(void)getPoints
{
//get Document directory's path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"POINTS.txt"];
//read points array from file
NSMutableArray *points = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSString *str = [points componentsJoinedByString:@","];
NSLog(@"%@", str);
}