Post

Replies

Boosts

Views

Activity

Problem Decoding using Swift Codable
I am new to Swift and not very experienced as a programmer in general. My question is what am I doing wrong with decoding data?I seem to have no problem encoding and writing data to disk but when I read and decode that same data, it is having a problem.import Foundation class Person: Codable { var name: String init(name: String){ self.name = name } } let aPerson = Person(name: "John") let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let archiveURL = documentsDirectory.appendingPathComponent("name_test").appendingPathExtension("plist") let propertyListEncoder = PropertyListEncoder() let encodedPerson = try? propertyListEncoder.encode(aPerson) try? encodedPerson?.write(to: archiveURL) let propertyListDecoder = PropertyListDecoder() if let retrievedPersonData = try? Data(contentsOf: archiveURL), let decodedPerson = try? propertyListDecoder.decode(Person.self, from: retrievedPersonData) { print(decodedPerson) }The output is: __lldb_expr_X.Person (where X is an odd number that will start at 1 and rise after every subsequent run).Any ideas?
2
0
1.1k
Dec ’19