Example (preferably simple) subclass of NSCoder?

I'm trying to subclass NSCoder, but I really don't know where to start. Apple's documentation lists which methods are needed, but not much more. My Google-fu may be weak, but I cannot find any implementation examples, for example. encodeValueOfObjCType:at: anywhere. (Although I suppose this is due to many cases.)

Does anyone know an example subclass of NSCoder that I can look at, or have an idea of ​​what a case or two from encodeValueOfObjCType:at: and decodeValueOfObjCType:at: :? Should look like?

+4
source share
2 answers

I am only opening a subclass of NSCoder here. This is basically a replica of the deprecated NSArchiver . Must make everyone who stumble over this issue has begun.

+1
source

I also wanted to use NSCoder to create simpler XML than what NSKeyedArchiver produces and implements some classes for it. The classes are called RWPlainXMLTreeEncoder and RWPlainXMLTreeDecoder , and I wrote some test code for them .

RWPlainXMLTreeEncoder assumes that the graph of the object you are encoding is a tree (in case the same object is twice encoded, the decoded tree will contain two different copies instead of one common copy; if you try to encode a cyclic graph, an exception). It generates an XML element on the encoded object, which looks approximately the same as for this example, encoding an array containing the string "A string":

 <ROOT type="@NSArray"><NS.object.0 type="@NSString"><NS.bytes>4120737472696E67</NS.bytes></NS.object.0></ROOT> 

I would like to further improve the above by using a different method instead of my own encodeWithCoder: object encodeWithCoder: for objects like arrays and strings, so above it will be:

 <ROOT type="array"><item.0 type="string">A string</item.0></array></ROOT> 

However, I am not sure if I will continue to work on this. My common goal was to have a fairly general simple way to save a tree of objects to a file that uses the encodeWithCoder: methods that I already wrote when creating a file that is not Cocoa-dependent, as when using NSKeyedArchiver. This will allow other users to write applications that open these files on other platforms.

But now I realized that there were similar efforts , which may already be more advanced, and, in addition, since XML is a document markup language, it may not be the best target format, and a language without markup may be better suited.

However, if you want to continue this or have another reason to look at a fairly simple subclass of NSCoder, feel free to use my code. You can also take a look at MAKeyedArchiver . Oh, and my code is covered in a BSD-style license (at least the version that is in SVN version 424 , I can change this for future versions). Improvements and feedback are welcome.

+1
source

Source: https://habr.com/ru/post/1302629/


All Articles