JSONEncoder, JSONDecoder, NSKeyedArchiver and NSKeyedUnarchiver in Swift
Introduction
In Swift, there are several ways to convert an object into a format that can be stored or transmitted. Two commonly used classes in the Foundation framework are NSKeyedArchiver and JSONEncoder, and their counterparts NSKeyedUnarchiver and JSONDecoder. In this article, we will discuss the differences between these two pairs of classes and when to use them.
NSKeyedArchiver and NSKeyedUnarchiver
NSKeyedArchiver and NSKeyedUnarchiver are classes that allow you to convert Swift objects to and from binary format. They require custom classes to implement the NSCoding protocol, which defines two methods: encode(with:) and init(coder:). The encode(with:) method is called by NSKeyedArchiver to encode the object's properties, while the init(coder:) method is called by NSKeyedUnarchiver to decode the object's properties.
NSKeyedArchiver works by serializing objects into a binary format that can be stored or transmitted. To use NSKeyedArchiver, you need to implement the NSCoding protocol in your custom classes. The resulting binary data can be sent over the network or stored in a file.
NSKeyedUnarchiver works in the opposite direction, deserializing binary data into objects. You can use NSKeyedUnarchiver to retrieve data that was previously stored in a file or transmitted over the network.
NSKeyedArchiver and NSKeyedUnarchiver are useful when you need to store or transmit binary data, and when you want to customize the encoding and decoding process for your objects. However, they are not suitable for situations where human-readable data is required.
Here's an example for NSKeyedArchiver / NSKeyedUnarchiver:
class Person: NSObject, NSCoding { var name: String var age: Int init(name: String, age: Int) { self.name = name self.age = age } func encode(with coder: NSCoder) { coder.encode(name, forKey: "name") coder.encode(age, forKey: "age") } required init?(coder: NSCoder) { self.name = coder.decodeObject(forKey: "name") as? String ?? "" self.age = coder.decodeInteger(forKey: "age") } } // Encoding the object into binary data let person = Person(name: "John", age: 30) let encodedData = NSKeyedArchiver.archivedData(withRootObject: person) // Decoding the binary data back into an object if let decodedPerson = NSKeyedUnarchiver.unarchiveObject(with: encodedData) as? Person { print("Name: \(decodedPerson.name), Age: \(decodedPerson.age)") }
JSONEncoder and JSONDecoder
JSONEncoder and JSONDecoder are classes that allow you to convert Swift objects to and from JSON format. They require custom classes to implement the Codable protocol, which combines the functionalities of the Encodable and Decodable protocols. The Encodable protocol defines a single method encode(to:), which is called by JSONEncoder to encode the object's properties, while the Decodable protocol defines a single method init(from:), which is called by JSONDecoder to decode the object's properties.
JSONEncoder works by encoding objects into JSON format. The resulting JSON data can be sent over the network or stored in a file, and can be easily read by humans.
JSONDecoder works in the opposite direction, decoding JSON data into objects. You can use JSONDecoder to retrieve data that was previously stored in JSON format, such as data retrieved from a web API.
JSONEncoder and JSONDecoder are useful when you need to work with human-readable data, or when you want to easily transmit data over the network. They are not suitable for situations where binary data is required.
Here's an example for JSONEncoder:
struct Person: Codable { var name: String var age: Int } let person = Person(name: "Alice", age: 25) let encoder = JSONEncoder() let data = try encoder.encode(person) print(String(data: data, encoding: .utf8)!) // Output: {"name":"Alice","age":25}
Here's an example for JSONDecoder:
let json = """ { "name": "Alice", "age": 25 } """ let decoder = JSONDecoder() let data = json.data(using: .utf8)! let person = try decoder.decode(Person.self, from: data) print(person) // Output: Person(name: "Alice", age: 25)
Conclusion
NSKeyedArchiver and NSKeyedUnarchiver are useful when you need to store or transmit binary data and want to customize the encoding and decoding process for your objects. JSONEncoder and JSONDecoder are useful when you need to work with human-readable data or want to easily transmit data over the network. Understanding the differences between these two pairs of classes is important for choosing the appropriate encoding and decoding method for your use case.
댓글
댓글 쓰기