How to use singleValueContainer method of _PlistEncoder class

Best Swift-snapshot-testing code snippet using _PlistEncoder.singleValueContainer

PlistEncoder.swift

Source:PlistEncoder.swift Github

copy

Full Screen

...138 topContainer = container139 }140 return _PlistUnkeyedEncodingContainer(referencing: self, codingPath: self.codingPath, wrapping: topContainer)141 }142 public func singleValueContainer() -> SingleValueEncodingContainer {143 return self144 }145}146// MARK: - Encoding Storage and Containers147fileprivate struct _PlistEncodingStorage {148 // MARK: Properties149 /// The container stack.150 /// Elements may be any one of the plist types (NSNumber, NSString, NSDate, NSArray, NSDictionary).151 private(set) fileprivate var containers: [NSObject] = []152 // MARK: - Initialization153 /// Initializes `self` with no containers.154 fileprivate init() {}155 // MARK: - Modifying the Stack156 fileprivate var count: Int {157 return self.containers.count158 }159 fileprivate mutating func pushKeyedContainer() -> NSMutableDictionary {160 let dictionary = NSMutableDictionary()161 self.containers.append(dictionary)162 return dictionary163 }164 fileprivate mutating func pushUnkeyedContainer() -> NSMutableArray {165 let array = NSMutableArray()166 self.containers.append(array)167 return array168 }169 fileprivate mutating func push(container: NSObject) {170 self.containers.append(container)171 }172 fileprivate mutating func popContainer() -> NSObject {173 precondition(!self.containers.isEmpty, "Empty container stack.")174 return self.containers.popLast()!175 }176}177// MARK: - Encoding Containers178fileprivate struct _PlistKeyedEncodingContainer<K : CodingKey> : KeyedEncodingContainerProtocol {179 typealias Key = K180 // MARK: Properties181 /// A reference to the encoder we're writing to.182 private let encoder: _PlistEncoder183 /// A reference to the container we're writing to.184 private let container: NSMutableDictionary185 /// The path of coding keys taken to get to this point in encoding.186 private(set) public var codingPath: [CodingKey]187 // MARK: - Initialization188 /// Initializes `self` with the given references.189 fileprivate init(referencing encoder: _PlistEncoder, codingPath: [CodingKey], wrapping container: NSMutableDictionary) {190 self.encoder = encoder191 self.codingPath = codingPath192 self.container = container193 }194 // MARK: - KeyedEncodingContainerProtocol Methods195 public mutating func encodeNil(forKey key: Key) throws { self.container[key.stringValue] = _plistNullNSString }196 public mutating func encode(_ value: Bool, forKey key: Key) throws { self.container[key.stringValue] = self.encoder.box(value) }197 public mutating func encode(_ value: Int, forKey key: Key) throws { self.container[key.stringValue] = self.encoder.box(value) }198 public mutating func encode(_ value: Int8, forKey key: Key) throws { self.container[key.stringValue] = self.encoder.box(value) }199 public mutating func encode(_ value: Int16, forKey key: Key) throws { self.container[key.stringValue] = self.encoder.box(value) }200 public mutating func encode(_ value: Int32, forKey key: Key) throws { self.container[key.stringValue] = self.encoder.box(value) }201 public mutating func encode(_ value: Int64, forKey key: Key) throws { self.container[key.stringValue] = self.encoder.box(value) }202 public mutating func encode(_ value: UInt, forKey key: Key) throws { self.container[key.stringValue] = self.encoder.box(value) }203 public mutating func encode(_ value: UInt8, forKey key: Key) throws { self.container[key.stringValue] = self.encoder.box(value) }204 public mutating func encode(_ value: UInt16, forKey key: Key) throws { self.container[key.stringValue] = self.encoder.box(value) }205 public mutating func encode(_ value: UInt32, forKey key: Key) throws { self.container[key.stringValue] = self.encoder.box(value) }206 public mutating func encode(_ value: UInt64, forKey key: Key) throws { self.container[key.stringValue] = self.encoder.box(value) }207 public mutating func encode(_ value: String, forKey key: Key) throws { self.container[key.stringValue] = self.encoder.box(value) }208 public mutating func encode(_ value: Float, forKey key: Key) throws { self.container[key.stringValue] = self.encoder.box(value) }209 public mutating func encode(_ value: Double, forKey key: Key) throws { self.container[key.stringValue] = self.encoder.box(value) }210 public mutating func encode<T : Encodable>(_ value: T, forKey key: Key) throws {211 self.encoder.codingPath.append(key)212 defer { self.encoder.codingPath.removeLast() }213 self.container[key.stringValue] = try self.encoder.box(value)214 }215 public mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: Key) -> KeyedEncodingContainer<NestedKey> {216 let dictionary = NSMutableDictionary()217 self.container[key.stringValue] = dictionary218 self.codingPath.append(key)219 defer { self.codingPath.removeLast() }220 let container = _PlistKeyedEncodingContainer<NestedKey>(referencing: self.encoder, codingPath: self.codingPath, wrapping: dictionary)221 return KeyedEncodingContainer(container)222 }223 public mutating func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer {224 let array = NSMutableArray()225 self.container[key.stringValue] = array226 self.codingPath.append(key)227 defer { self.codingPath.removeLast() }228 return _PlistUnkeyedEncodingContainer(referencing: self.encoder, codingPath: self.codingPath, wrapping: array)229 }230 public mutating func superEncoder() -> Encoder {231 return _PlistReferencingEncoder(referencing: self.encoder, at: _PlistKey.super, wrapping: self.container)232 }233 public mutating func superEncoder(forKey key: Key) -> Encoder {234 return _PlistReferencingEncoder(referencing: self.encoder, at: key, wrapping: self.container)235 }236}237fileprivate struct _PlistUnkeyedEncodingContainer : UnkeyedEncodingContainer {238 // MARK: Properties239 /// A reference to the encoder we're writing to.240 private let encoder: _PlistEncoder241 /// A reference to the container we're writing to.242 private let container: NSMutableArray243 /// The path of coding keys taken to get to this point in encoding.244 private(set) public var codingPath: [CodingKey]245 /// The number of elements encoded into the container.246 public var count: Int {247 return self.container.count248 }249 // MARK: - Initialization250 /// Initializes `self` with the given references.251 fileprivate init(referencing encoder: _PlistEncoder, codingPath: [CodingKey], wrapping container: NSMutableArray) {252 self.encoder = encoder253 self.codingPath = codingPath254 self.container = container255 }256 // MARK: - UnkeyedEncodingContainer Methods257 public mutating func encodeNil() throws { self.container.add(_plistNullNSString) }258 public mutating func encode(_ value: Bool) throws { self.container.add(self.encoder.box(value)) }259 public mutating func encode(_ value: Int) throws { self.container.add(self.encoder.box(value)) }260 public mutating func encode(_ value: Int8) throws { self.container.add(self.encoder.box(value)) }261 public mutating func encode(_ value: Int16) throws { self.container.add(self.encoder.box(value)) }262 public mutating func encode(_ value: Int32) throws { self.container.add(self.encoder.box(value)) }263 public mutating func encode(_ value: Int64) throws { self.container.add(self.encoder.box(value)) }264 public mutating func encode(_ value: UInt) throws { self.container.add(self.encoder.box(value)) }265 public mutating func encode(_ value: UInt8) throws { self.container.add(self.encoder.box(value)) }266 public mutating func encode(_ value: UInt16) throws { self.container.add(self.encoder.box(value)) }267 public mutating func encode(_ value: UInt32) throws { self.container.add(self.encoder.box(value)) }268 public mutating func encode(_ value: UInt64) throws { self.container.add(self.encoder.box(value)) }269 public mutating func encode(_ value: Float) throws { self.container.add(self.encoder.box(value)) }270 public mutating func encode(_ value: Double) throws { self.container.add(self.encoder.box(value)) }271 public mutating func encode(_ value: String) throws { self.container.add(self.encoder.box(value)) }272 public mutating func encode<T : Encodable>(_ value: T) throws {273 self.encoder.codingPath.append(_PlistKey(index: self.count))274 defer { self.encoder.codingPath.removeLast() }275 self.container.add(try self.encoder.box(value))276 }277 public mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type) -> KeyedEncodingContainer<NestedKey> {278 self.codingPath.append(_PlistKey(index: self.count))279 defer { self.codingPath.removeLast() }280 let dictionary = NSMutableDictionary()281 self.container.add(dictionary)282 let container = _PlistKeyedEncodingContainer<NestedKey>(referencing: self.encoder, codingPath: self.codingPath, wrapping: dictionary)283 return KeyedEncodingContainer(container)284 }285 public mutating func nestedUnkeyedContainer() -> UnkeyedEncodingContainer {286 self.codingPath.append(_PlistKey(index: self.count))287 defer { self.codingPath.removeLast() }288 let array = NSMutableArray()289 self.container.add(array)290 return _PlistUnkeyedEncodingContainer(referencing: self.encoder, codingPath: self.codingPath, wrapping: array)291 }292 public mutating func superEncoder() -> Encoder {293 return _PlistReferencingEncoder(referencing: self.encoder, at: self.container.count, wrapping: self.container)294 }295}296extension _PlistEncoder : SingleValueEncodingContainer {297 // MARK: - SingleValueEncodingContainer Methods298 private func assertCanEncodeNewValue() {299 precondition(self.canEncodeNewValue, "Attempt to encode value through single value container when previously value already encoded.")300 }301 public func encodeNil() throws {302 assertCanEncodeNewValue()303 self.storage.push(container: _plistNullNSString)304 }305 public func encode(_ value: Bool) throws {306 assertCanEncodeNewValue()307 self.storage.push(container: self.box(value))308 }309 public func encode(_ value: Int) throws {310 assertCanEncodeNewValue()311 self.storage.push(container: self.box(value))312 }313 public func encode(_ value: Int8) throws {314 assertCanEncodeNewValue()315 self.storage.push(container: self.box(value))316 }317 public func encode(_ value: Int16) throws {318 assertCanEncodeNewValue()319 self.storage.push(container: self.box(value))320 }321 public func encode(_ value: Int32) throws {322 assertCanEncodeNewValue()323 self.storage.push(container: self.box(value))324 }325 public func encode(_ value: Int64) throws {326 assertCanEncodeNewValue()327 self.storage.push(container: self.box(value))328 }329 public func encode(_ value: UInt) throws {330 assertCanEncodeNewValue()331 self.storage.push(container: self.box(value))332 }333 public func encode(_ value: UInt8) throws {334 assertCanEncodeNewValue()335 self.storage.push(container: self.box(value))336 }337 public func encode(_ value: UInt16) throws {338 assertCanEncodeNewValue()339 self.storage.push(container: self.box(value))340 }341 public func encode(_ value: UInt32) throws {342 assertCanEncodeNewValue()343 self.storage.push(container: self.box(value))344 }345 public func encode(_ value: UInt64) throws {346 assertCanEncodeNewValue()347 self.storage.push(container: self.box(value))348 }349 public func encode(_ value: String) throws {350 assertCanEncodeNewValue()351 self.storage.push(container: self.box(value))352 }353 public func encode(_ value: Float) throws {354 assertCanEncodeNewValue()355 self.storage.push(container: self.box(value))356 }357 public func encode(_ value: Double) throws {358 assertCanEncodeNewValue()359 self.storage.push(container: self.box(value))360 }361 public func encode<T : Encodable>(_ value: T) throws {362 assertCanEncodeNewValue()363 try self.storage.push(container: self.box(value))364 }365}366// MARK: - Concrete Value Representations367extension _PlistEncoder {368 /// Returns the given value boxed in a container appropriate for pushing onto the container stack.369 fileprivate func box(_ value: Bool) -> NSObject { return NSNumber(value: value) }370 fileprivate func box(_ value: Int) -> NSObject { return NSNumber(value: value) }371 fileprivate func box(_ value: Int8) -> NSObject { return NSNumber(value: value) }372 fileprivate func box(_ value: Int16) -> NSObject { return NSNumber(value: value) }373 fileprivate func box(_ value: Int32) -> NSObject { return NSNumber(value: value) }374 fileprivate func box(_ value: Int64) -> NSObject { return NSNumber(value: value) }375 fileprivate func box(_ value: UInt) -> NSObject { return NSNumber(value: value) }376 fileprivate func box(_ value: UInt8) -> NSObject { return NSNumber(value: value) }377 fileprivate func box(_ value: UInt16) -> NSObject { return NSNumber(value: value) }378 fileprivate func box(_ value: UInt32) -> NSObject { return NSNumber(value: value) }379 fileprivate func box(_ value: UInt64) -> NSObject { return NSNumber(value: value) }380 fileprivate func box(_ value: Float) -> NSObject { return NSNumber(value: value) }381 fileprivate func box(_ value: Double) -> NSObject { return NSNumber(value: value) }382 fileprivate func box(_ value: String) -> NSObject { return NSString(string: value) }383 fileprivate func box<T : Encodable>(_ value: T) throws -> NSObject {384 return try self.box_(value) ?? NSDictionary()385 }386 fileprivate func box_<T : Encodable>(_ value: T) throws -> NSObject? {387 if T.self == Date.self || T.self == NSDate.self {388 // PropertyListSerialization handles NSDate directly.389 return (value as! NSDate)390 } else if T.self == Data.self || T.self == NSData.self {391 // PropertyListSerialization handles NSData directly.392 return (value as! NSData)393 }394 // The value should request a container from the _PlistEncoder.395 let depth = self.storage.count396 do {397 try value.encode(to: self)398 } catch let error {399 // If the value pushed a container before throwing, pop it back off to restore state.400 if self.storage.count > depth {401 let _ = self.storage.popContainer()402 }403 throw error404 }405 // The top container should be a new container.406 guard self.storage.count > depth else {407 return nil408 }409 return self.storage.popContainer()410 }411}412// MARK: - _PlistReferencingEncoder413/// _PlistReferencingEncoder is a special subclass of _PlistEncoder which has its own storage, but references the contents of a different encoder.414/// It's used in superEncoder(), which returns a new encoder for encoding a superclass -- the lifetime of the encoder should not escape the scope it's created in, but it doesn't necessarily know when it's done being used (to write to the original container).415fileprivate class _PlistReferencingEncoder : _PlistEncoder {416 // MARK: Reference types.417 /// The type of container we're referencing.418 private enum Reference {419 /// Referencing a specific index in an array container.420 case array(NSMutableArray, Int)421 /// Referencing a specific key in a dictionary container.422 case dictionary(NSMutableDictionary, String)423 }424 // MARK: - Properties425 /// The encoder we're referencing.426 private let encoder: _PlistEncoder427 /// The container reference itself.428 private let reference: Reference429 // MARK: - Initialization430 /// Initializes `self` by referencing the given array container in the given encoder.431 fileprivate init(referencing encoder: _PlistEncoder, at index: Int, wrapping array: NSMutableArray) {432 self.encoder = encoder433 self.reference = .array(array, index)434 super.init(options: encoder.options, codingPath: encoder.codingPath)435 self.codingPath.append(_PlistKey(index: index))436 }437 /// Initializes `self` by referencing the given dictionary container in the given encoder.438 fileprivate init(referencing encoder: _PlistEncoder, at key: CodingKey, wrapping dictionary: NSMutableDictionary) {439 self.encoder = encoder440 self.reference = .dictionary(dictionary, key.stringValue)441 super.init(options: encoder.options, codingPath: encoder.codingPath)442 self.codingPath.append(key)443 }444 // MARK: - Coding Path Operations445 fileprivate override var canEncodeNewValue: Bool {446 // With a regular encoder, the storage and coding path grow together.447 // A referencing encoder, however, inherits its parents coding path, as well as the key it was created for.448 // We have to take this into account.449 return self.storage.count == self.codingPath.count - self.encoder.codingPath.count - 1450 }451 // MARK: - Deinitialization452 // Finalizes `self` by writing the contents of our storage to the referenced encoder's storage.453 deinit {454 let value: Any455 switch self.storage.count {456 case 0: value = NSDictionary()457 case 1: value = self.storage.popContainer()458 default: fatalError("Referencing encoder deallocated with multiple containers on stack.")459 }460 switch self.reference {461 case .array(let array, let index):462 array.insert(value, at: index)463 case .dictionary(let dictionary, let key):464 dictionary[NSString(string: key)] = value465 }466 }467}468//===----------------------------------------------------------------------===//469// Plist Decoder470//===----------------------------------------------------------------------===//471/// `PropertyListDecoder` facilitates the decoding of property list values into semantic `Decodable` types.472open class PropertyListDecoder {473 // MARK: Options474 /// Contextual user-provided information for use during decoding.475 open var userInfo: [CodingUserInfoKey : Any] = [:]476 /// Options set on the top-level encoder to pass down the decoding hierarchy.477 fileprivate struct _Options {478 let userInfo: [CodingUserInfoKey : Any]479 }480 /// The options set on the top-level decoder.481 fileprivate var options: _Options {482 return _Options(userInfo: userInfo)483 }484 // MARK: - Constructing a Property List Decoder485 /// Initializes `self` with default strategies.486 public init() {}487 // MARK: - Decoding Values488 /// Decodes a top-level value of the given type from the given property list representation.489 ///490 /// - parameter type: The type of the value to decode.491 /// - parameter data: The data to decode from.492 /// - returns: A value of the requested type.493 /// - throws: `DecodingError.dataCorrupted` if values requested from the payload are corrupted, or if the given data is not a valid property list.494 /// - throws: An error if any value throws an error during decoding.495 open func decode<T : Decodable>(_ type: T.Type, from data: Data) throws -> T {496 var format: PropertyListSerialization.PropertyListFormat = .binary497 return try decode(type, from: data, format: &format)498 }499 /// Decodes a top-level value of the given type from the given property list representation.500 ///501 /// - parameter type: The type of the value to decode.502 /// - parameter data: The data to decode from.503 /// - parameter format: The parsed property list format.504 /// - returns: A value of the requested type along with the detected format of the property list.505 /// - throws: `DecodingError.dataCorrupted` if values requested from the payload are corrupted, or if the given data is not a valid property list.506 /// - throws: An error if any value throws an error during decoding.507 open func decode<T : Decodable>(_ type: T.Type, from data: Data, format: inout PropertyListSerialization.PropertyListFormat) throws -> T {508 let topLevel: Any509 do {510 topLevel = try PropertyListSerialization.propertyList(from: data, options: [], format: &format)511 } catch {512 throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [], debugDescription: "The given data was not a valid property list.", underlyingError: error))513 }514 return try decode(type, fromTopLevel: topLevel)515 }516 /// Decodes a top-level value of the given type from the given property list container (top-level array or dictionary).517 ///518 /// - parameter type: The type of the value to decode.519 /// - parameter container: The top-level plist container.520 /// - returns: A value of the requested type.521 /// - throws: `DecodingError.dataCorrupted` if values requested from the payload are corrupted, or if the given data is not a valid property list.522 /// - throws: An error if any value throws an error during decoding.523 internal func decode<T : Decodable>(_ type: T.Type, fromTopLevel container: Any) throws -> T {524 let decoder = _PlistDecoder(referencing: container, options: self.options)525 guard let value = try decoder.unbox(container, as: type) else {526 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: [], debugDescription: "The given data did not contain a top-level value."))527 }528 return value529 }530}531// MARK: - _PlistDecoder532fileprivate class _PlistDecoder : Decoder {533 // MARK: Properties534 /// The decoder's storage.535 fileprivate var storage: _PlistDecodingStorage536 /// Options set on the top-level decoder.537 fileprivate let options: PropertyListDecoder._Options538 /// The path to the current point in encoding.539 fileprivate(set) public var codingPath: [CodingKey]540 /// Contextual user-provided information for use during encoding.541 public var userInfo: [CodingUserInfoKey : Any] {542 return self.options.userInfo543 }544 // MARK: - Initialization545 /// Initializes `self` with the given top-level container and options.546 fileprivate init(referencing container: Any, at codingPath: [CodingKey] = [], options: PropertyListDecoder._Options) {547 self.storage = _PlistDecodingStorage()548 self.storage.push(container: container)549 self.codingPath = codingPath550 self.options = options551 }552 // MARK: - Decoder Methods553 public func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> {554 guard !(self.storage.topContainer is NSNull) else {555 throw DecodingError.valueNotFound(KeyedDecodingContainer<Key>.self,556 DecodingError.Context(codingPath: self.codingPath,557 debugDescription: "Cannot get keyed decoding container -- found null value instead."))558 }559 guard let topContainer = self.storage.topContainer as? [String : Any] else {560 throw DecodingError._typeMismatch(at: self.codingPath, expectation: [String : Any].self, reality: self.storage.topContainer)561 }562 let container = _PlistKeyedDecodingContainer<Key>(referencing: self, wrapping: topContainer)563 return KeyedDecodingContainer(container)564 }565 public func unkeyedContainer() throws -> UnkeyedDecodingContainer {566 guard !(self.storage.topContainer is NSNull) else {567 throw DecodingError.valueNotFound(UnkeyedDecodingContainer.self,568 DecodingError.Context(codingPath: self.codingPath,569 debugDescription: "Cannot get unkeyed decoding container -- found null value instead."))570 }571 guard let topContainer = self.storage.topContainer as? [Any] else {572 throw DecodingError._typeMismatch(at: self.codingPath, expectation: [Any].self, reality: self.storage.topContainer)573 }574 return _PlistUnkeyedDecodingContainer(referencing: self, wrapping: topContainer)575 }576 public func singleValueContainer() throws -> SingleValueDecodingContainer {577 return self578 }579}580// MARK: - Decoding Storage581fileprivate struct _PlistDecodingStorage {582 // MARK: Properties583 /// The container stack.584 /// Elements may be any one of the plist types (NSNumber, Date, String, Array, [String : Any]).585 private(set) fileprivate var containers: [Any] = []586 // MARK: - Initialization587 /// Initializes `self` with no containers.588 fileprivate init() {}589 // MARK: - Modifying the Stack590 fileprivate var count: Int {...

Full Screen

Full Screen

singleValueContainer

Using AI Code Generation

copy

Full Screen

1import Foundation2struct Person: Codable {3}4let person = Person(name: "John", age: 21)5let encoder = PropertyListEncoder()6let data = try encoder.encode(person)7import Foundation8struct Person: Codable {9}10""".data(using: .utf8)!11let decoder = PropertyListDecoder()12let person = try decoder.decode(Person.self, from: data)13import Foundation14struct Person: Codable {15}16let person = Person(name: "John", age: 21)17let encoder = PropertyListEncoder()18let data = try encoder.encode([person])19import Foundation20struct Person: Codable {21}22""".data(using: .utf8)!23let decoder = PropertyListDecoder()24let person = try decoder.decode([Person].self, from: data)25import Foundation26struct Person: Codable {27}28let person = Person(name: "John", age: 21)

Full Screen

Full Screen

singleValueContainer

Using AI Code Generation

copy

Full Screen

1let encoder = PropertyListEncoder()2let encoded = try encoder.encode(1)3let plist = try PropertyListSerialization.propertyList(from: encoded, options: [], format: nil)4print(plist)5let encoder = PropertyListEncoder()6let encoded = try encoder.encode(1)7let plist = try PropertyListSerialization.propertyList(from: encoded, options: [], format: nil)8print(plist)9let encoder = PropertyListEncoder()10let encoded = try encoder.encode(1)11let plist = try PropertyListSerialization.propertyList(from: encoded, options: [], format: nil)12print(plist)13let encoder = PropertyListEncoder()14let encoded = try encoder.encode(1)15let plist = try PropertyListSerialization.propertyList(from: encoded, options: [], format: nil)16print(plist)17let encoder = PropertyListEncoder()18let encoded = try encoder.encode(1)19let plist = try PropertyListSerialization.propertyList(from: encoded, options: [], format: nil)20print(plist)21let encoder = PropertyListEncoder()22let encoded = try encoder.encode(1)23let plist = try PropertyListSerialization.propertyList(from: encoded, options: [], format: nil)24print(plist)25let encoder = PropertyListEncoder()26let encoded = try encoder.encode(1)27let plist = try PropertyListSerialization.propertyList(from: encoded, options: [], format: nil)28print(plist)29let encoder = PropertyListEncoder()30let encoded = try encoder.encode(1)31let plist = try PropertyListSerialization.propertyList(from: encoded, options: [], format: nil)32print(plist)33let encoder = PropertyListEncoder()

Full Screen

Full Screen

singleValueContainer

Using AI Code Generation

copy

Full Screen

1import Foundation2struct Employee {3}4let employee = Employee(name: "John", age: 30)5let encoder = PropertyListEncoder()6let data = try encoder.encode(employee)7print(String(data: data, encoding: .utf8)!)8import Foundation9struct Employee {10}11let employee = Employee(name: "John", age: 30)12let encoder = PropertyListEncoder()13let data = try encoder.encode(employee)14print(String(data: data, encoding: .utf8)!)15import Foundation16struct Employee {17}18let employee = Employee(name: "John", age: 30)19let encoder = PropertyListEncoder()20let data = try encoder.encode(employee)21print(String(data: data, encoding: .utf8)!)22import Foundation23struct Employee {24}25let employee = Employee(name: "John", age: 30)26let encoder = PropertyListEncoder()27let data = try encoder.encode(employee)28print(String(data: data, encoding: .utf8)!)29import Foundation30struct Employee {31}32let employee = Employee(name: "John", age: 30)33let encoder = PropertyListEncoder()34let data = try encoder.encode(employee)35print(String(data: data, encoding: .utf8)!)36import Foundation37struct Employee {

Full Screen

Full Screen

singleValueContainer

Using AI Code Generation

copy

Full Screen

1import Foundation2class Person: Codable {3}4let person = Person(name: "John", age: 30, isMarried: true, weight: 70.5, height: 5.6)5let encoder = PropertyListEncoder()6do {7 let data = try encoder.encode(person)8 let plist = try PropertyListSerialization.propertyList(from: data, options: [], format: nil)9 print(plist)10} catch {11 print(error)12}13{14 age = 30;15 height = "5.6";16 isMarried = 1;17 name = John;18 weight = "70.5";19}

Full Screen

Full Screen

singleValueContainer

Using AI Code Generation

copy

Full Screen

1import Foundation2class A : Codable {3 init(name : String) {4 }5}6let a = A(name: "swift")7let encoder = PropertyListEncoder()8do {9 let data = try encoder.encode(a)10 let str = String(data: data, encoding: .utf8)11 print(str!)12} catch {13 print(error)14}15import Foundation16class A : Codable {17 init(name : String) {18 }19}20let a = A(name: "swift")21let encoder = PropertyListEncoder()22do {23 let data = try encoder.encode(a)24 let str = String(data: data, encoding: .utf8)25 print(str!)26} catch {27 print(error)28}29import Foundation30class A : Codable {31 init(name : String) {32 }33}34let a = A(name: "swift")35let encoder = PropertyListEncoder()36do {37 let data = try encoder.encode(a)38 let str = String(data: data, encoding: .utf8)39 print(str!)40} catch {41 print(error)42}43import Foundation44class A : Codable {45 init(name : String) {46 }47}48let a = A(name: "swift")49let encoder = PropertyListEncoder()50do {51 let data = try encoder.encode(a)52 let str = String(data: data, encoding:

Full Screen

Full Screen

singleValueContainer

Using AI Code Generation

copy

Full Screen

1import Foundation2let encoder = _PlistEncoder()3let data = try! encoder.encode(["Hello"])4import Foundation5let decoder = _PlistDecoder()6let data = try! decoder.decode([String].self, from: Data())7import Foundation8let encoder = _PlistEncoder()9let decoder = _PlistDecoder()10let data = try! encoder.encode(["Hello"])11let decodedData = try! decoder.decode([String].self, from: data)12import Foundation13let encoder = _PlistEncoder()14let data = try! encoder.encode([1, 2, 3])15let decoder = _PlistDecoder()16let decodedData = try! decoder.decode([Int].self, from: data)17import Foundation18let encoder = _PlistEncoder()19let data = try! encoder.encode([1, 2.0, 3])20let decoder = _PlistDecoder()21let decodedData = try! decoder.decode([Any].self, from: data)22import Foundation23let encoder = _PlistEncoder()24let data = try! encoder.encode([1, 2.0, "Hello"])25let decoder = _PlistDecoder()26let decodedData = try! decoder.decode([Any].self, from: data)27import Foundation28let encoder = _PlistEncoder()29let data = try! encoder.encode(["Hello": 1])30let decoder = _PlistDecoder()31let decodedData = try! decoder.decode([String: Int].self, from: data)32import Foundation33let encoder = _PlistEncoder()34let data = try! encoder.encode(["Hello": 1, "World": 2.0])35let decoder = _PlistDecoder()36let decodedData = try! decoder.decode([String: Any].self, from: data)37import Foundation38let encoder = _PlistEncoder()39let data = try! encoder.encode(["Hello": 1, "World": 2.0, "Hi": "There"])40let decoder = _PlistDecoder()41let decodedData = try! decoder.decode([String: Any].self, from: data)42import Foundation43let encoder = _PlistEncoder()44let data = try! encoder.encode(["

Full Screen

Full Screen

singleValueContainer

Using AI Code Generation

copy

Full Screen

1import Foundation2let encoder = PropertyListEncoder()3let data = try encoder.encode(str)4let fileURL = URL(fileURLWithPath: "1.plist")5try data.write(to: fileURL)6import Foundation7let decoder = PropertyListDecoder()8let fileURL = URL(fileURLWithPath: "1.plist")9let data = try Data(contentsOf: fileURL)10let str = try decoder.decode(String.self, from: data)11print(str)12import Foundation13let encoder = PropertyListEncoder()14let data = try encoder.encode(num)15let fileURL = URL(fileURLWithPath: "2.plist")16try data.write(to: fileURL)17import Foundation18let decoder = PropertyListDecoder()19let fileURL = URL(fileURLWithPath: "2.plist")20let data = try Data(contentsOf: fileURL)21let num = try decoder.decode(Int.self, from: data)22print(num)23import Foundation24let encoder = PropertyListEncoder()25let data = try encoder.encode(bool)26let fileURL = URL(fileURLWithPath: "3.plist")27try data.write(to: fileURL)28import Foundation29let decoder = PropertyListDecoder()30let fileURL = URL(fileURLWithPath: "3.plist")31let data = try Data(contentsOf: fileURL)32let bool = try decoder.decode(Bool.self, from: data)33print(bool)

Full Screen

Full Screen

singleValueContainer

Using AI Code Generation

copy

Full Screen

1import Foundation2struct Student {3}4let student = Student(name: "John", age: 25, city: "New York")5let plistEncoder = _PlistEncoder()6do {7 let encodedStudent = try plistEncoder.encode(student)8 print(encodedStudent)9} catch {10 print(error)11}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Swift-snapshot-testing automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in _PlistEncoder

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful