Reputation: 721
I have my own object class from inherited from NSObject
@interface BlockedCell : NSObject
{
NSValue *gridValue;
NSString *name;
}
@property (nonatomic, retain) NSValue *gridValue;
@property (nonatomic, retain) NSString *name;
@end
So I try to create a few objects:
NSMutableDictionary *dict = [[dict alloc] init];
for (int i = 0; i < 5; ++i)
{
BlockedCell *block = [[BlockedCell alloc] init];
block.gridValue = [NSValue valueWithCGPoint: CGPointMake(0.0f, 0.0f)];
block.name = @"something";
[dict setObject: block forKey: [NSString stringWithFormat: @"item_%d", i]];
[block release];
}
if([dict writeToFile: path atomic: YES])
NSLog(@"Saved");
else
NSLog(@"Failed to save");
[dict release];
And what I get for the output is "Failed to save".. If my dictionary does not contains any data, then it will output "Saved"
EDIT: After I did more testing, I found out that actually is the NSValue causing the saving failed. So what should I do if I want to save CGPoint into plist?
Upvotes: 0
Views: 1067
Reputation: 3345
you cant save NSValue directly.In your case you have to save a point in the form ofstring use below line
CGPoint point = CGPointMake(10.0,20.0) //you need to translate the point into a compatible "Plist" object such as NSString //luckily, there is a method for that
[rectArray addObject:NSStringFromPoint(point)];
//save into a plist .....
on retrieval of this value
CGPoint Point = CGPointFromString([rectArray objectAtIndex:0]);
Upvotes: 1
Reputation: 386038
As you discovered, property lists cannot store NSValue
objects directly. The supported classes are NSData, NSString, NSArray, NSDictionary, NSDate, and NSNumber, as documented in the NSPropertyListSerialization Class Reference.
The easiest workaround would be to use NSString
instead of NSValue
:
block.gridString = NSStringFromCGPoint(CGPointZero);
CGPoint point = CGPointFromString(block.gridString);
Upvotes: 2