0

I have an NSMutableArray with a bunch of Points(it's a class defined by me) inside. And I want to serialize this NSMutableArray to save to my disk. If I implement in my class Point the NSCoding protocol is it possible to use NSKeyedArchiver directly in my NSMutableArray?

Just like this example:

NSMutableData *data = [NSMutableData new];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:myArray forKey:@"array"];
[data writeToFile:filename attomically:YES];
[archiver release];
[data release];
1
  • 1
    should be fine as long as you implement NSCoding, NSCopying for Point correctly. Commented Apr 1, 2011 at 15:41

1 Answer 1

2

Yes it is possible, quoting Apple documentation :

The NSArray class adopts the NSCopying, NSMutableCopying, and NSCoding protocols;

Note that if you're working with points, maybe you should use the Apple CGPoint structure, which comes with many handy utilities : CGPointFromString and NSStringFromCGPoint see Apple documentation for more information.

And also these others ones might interest you CGPointEqualToPoint, CGRectContainsPoint, CGRectIntersectsRect or CGRectContainsRect... see Apple documentation for more information

Sign up to request clarification or add additional context in comments.

2 Comments

The problem is that i store more information than just position :( but thanx anyway for the answer
Then you could have your Point class having a coordinate property of type CGPoint. And your class could even expose x / y of the inner structure... That way you would get both aspects. Or why not just use CGPoint as temp vars when encoding/decoding...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.