CGpoint Array write into a text file
I have an array like this: CGPoint array[1000]
(I add location of touches into this array). The problem is that I want to save this array into a text file in the document directory and also ret开发者_如何学编程rieve it. Can anyone help with this?
Thanks All
Instead of using a plain C array, I would recommend that you use an NSArray
to store your collection of CGPoints. Since NSArray
conforms to the NSCoding
protocol, you can serialize and deserialize it from a file.
You should read up on Archives and Serializations Programming Guide.
EDIT Here's an example:
// Create an NSMutableArray
NSMutableArray *points = [[NSMutableArray alloc] init];
// Add a point to the array
// Read up on NSValue objects to understand why you simply cannot add a C
// struct like CGPoint to the array
[points addObject:[NSValue valueWithBytes:&point1 objCType:@encode(CGPoint)]];
// archive the array -- this will store the array in a file
BOOL result = [NSKeyedArchiver archiveRootObject:points toFile:filePath];
NSLog(@"Archival result: %d", result);
// unarchive the array -- this will retrieve your array from the file
NSMutableArray *points2 = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
精彩评论