![]() ![]() If an instance of some other class is inside the dictionary or array you want to encode serialization will fail. NSPropertyListSerialization works only with limited amount of Foundation classes: NSData, NSString, NSArray, NSDictionary, NSDate, and NSNumber. NSError * error NSPropertyListFormat format id plist = The class - an older brother of NSJSONSerialization - makes conversion between plist (which is represented in memory by instance of NSDictionary or NSArray) and binary data (represented by NSData). We have NSPropertyListSerialization (PropertyListSerialization when calling from Swift) which is being around since macOS 10.2/iOS 2. Generally plist serialization in Foundation world looks like JSON serialization. The format has appeared in NeXTSTEP operation system and made its way through decades to contemporary macOS/iOS ecosystem. Plist (from “property list” ) is a structured way to represent and persist a single object or an object tree. There are a number of libraries out there successfully using this approach (I had a pleasant experience using JSONModel). Optionally we can use some mapping tables and transformations if exact keys or values of the object properties are not what we want to see in JSON. So we can generalise the algorithm to serialise every object into JSON data. Here we get a list of all the properties of a given class with some attributes including the property type. #Obj c to swift converter freeThe implementation of the encoding method usually looks pretty much the same:įunc encodeWithCoder ( coder : NSCoder ) free ( properties ) You describe how your object supposed to be transferred into key-value pairs.Eventually some coder will serialise this container to binary data. Inside the implementation of this methods you specify how different properties of your object (the values inside these properties) are being encoded into some kind of key-value container. Your custom classes implement it to be ready for serialization/deserialization. NSCoding is lightweight ObjC protocol with two methods: -initWithCoder: and encodeWithCoder: which has been around since iOS 2. Let’s take a look at the most popular formats and different ways of handling serialization in iOS development. And then it doesn’t matter how this data supposed to be used: stored or passed somewhere it’s another separate processes. Serialization is the way to convert the objects the first system uses in its operation activity into this agreed format. There also is a protocol, an agreed data format expected on the receiver side. One system is a transmitter, another one is a receiver. ![]() (In case of saving/retrieving the data locally we can consider the same system in different points in time as two separate systems). The idea is that there are two (or even more) incapsulated systems separated from each other. But there are some other cases: for instance, passing the data to a third party library or transferring it to an external device via bluetooth. The most popular applications for serialization are local storing and sending the data over the internet. ![]() Until you take a look into the file you wrote on disc you have no idea the object was serialised into XML. Using this API you don’t control the process and you cannot adjust it. #Obj c to swift converter codeBut more importantly, serialization here inextricably coupled with persisting the data.ĭo you see serialization in the code snippet above? Me neither, but it’s implicitly inside. But there are some obvious limitations and downsides of this approach: you cannot use it for your custom classes, it doesn’t look nice in Swift with the type cast (even if you properly handle the errors). Here you serialise an object and store it to a file with just one line of code. Let hosts : = try ? ( hosts as NSArray ). For a long time we would naturally use this code to store NSArray or NSDictionary to file: Quite often serialization is only considered as a part of archiving data on disc. Serialization is the process of translating data structures or object state into a format that can be stored or transmitted and reconstructed later (possibly in a different computer environment). I will also compare ObjC approach to the problem (NSCoding) with the one we got in Swift 4 (Codable) and will take a look at some specific use cases like interop between them and the ability to work with complex object graphs. In this post I’ll try to systemise the knowledge about major formats and types of data serialization in iOS development. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |