cast a custom object to NSData for use with NSFileManager
I have an object that I've written in objective c. It is very simple and just really stores a few values.
My Object
@interface MyObject : NSObject {
NSString *filename;
NSMutableData *somedata;
}
@property(nonatomic,copy)NSStrin开发者_运维知识库g *filename;
@property(nonatomic,retain)NSMutableData *somedata;
@end
I want to store this object to disk using NSFileManager. I believe that NSFileManager takes an instance of NSData as a contents parameter when using createFileAtPath. How can I cast this Object to an appropriate type for using createFileAtPath?
all objects are from NSObject.here we can convert custom object which is derived from NSObject to NSData
You need to implement encodeWithCoder: on your custom class, serializing all of its attributes using the NSCoder passed into it. If its attributes include any more custom classes, they'll need encodeWithCoder: implementing too.
精彩评论