开发者

How to encode a non-C data type in Objective-c?

I am trying to encode the data type CGPDFPageRef like so (Source Code) but am getting the following error (Console Error Message):

Source Code:

//Get a CGPDFPageRef to page 20 of the current PDF document ref
CGPDFPageRef pageRef = CGPDFDocumentGetPage(myDocumentRef, 20);

//Set up the paths for writing
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"thisPage.wrapit"];

//Encode the CGPDFPageRef to the path
NSValue *pageRefObj = [NSValue value:&pageRef withObjCType:@encode(CGPDFPageRef)];
[NSKeyedArchiver archiveRootObject:pageRefObj toFile:path];

Console Error Message:

-[NSKeyedArchiver encodeValueOfObjCType:at:]: unknown type encoding ('^')'

My Theory开发者_StackOverflow中文版:

I think the error is because the type CGPDFPageRef is not recognized by the encoder. So...How can I encode/archive/store a non-object data type (non-C)? By the way, ANY way which would allow me to store a CGPDFPageRef to disk will suffice. Thank you.

Side-Note:

Apple Docs also state that variable-length data structures cannot be encoded? I don't know if this might be the problem either. PDFPages vary in size on disk, so would the CGPDFPageRef opaque type be considered variable-length? I dunno. Too bad it's opaque.


Your problem is that CGPDFPageRef is a pointer to an incomplete struct - the layout of the struct is unknown to the compiler so @encode(CGPDFPageRef) produces an invalid (incomplete) encoding.

You have to ask is it safe/meaningful to archive an opaque type? If the type itself doesn't provide archiving then you've no idea if it can be archived safely.

In this case I'd suggest you determine the document,page number pair and archive those, using that data on unarchiving to create a CGPDFPageRef.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜