开发者

cast a custom object to NSData for use with NSFileManager

开发者 https://www.devze.com 2023-03-13 07:53 出处:网络
I have an object that I\'ve written in objective c. It is very simple and just really stores a few values.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消