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?
As you discovered, property lists cannot store
NSValueobjects 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
NSStringinstead ofNSValue: