dubbeat
dubbeat

Reputation: 7847

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)NSString *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?

Upvotes: 1

Views: 963

Answers (1)

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.

Upvotes: 1

Related Questions