I conformed my following Person class to NSCoding protocol.
class Person: NSObject, NSCoding{
var age:Int
var height: Double
var name: String
init(age:Int, height: Double, name:String){
self.age = age
self.height = height
self.name = name
}
func encode(with aCoder: NSCoder){
aCoder.encode(age, forKey: "age")
aCoder.encode(height, forKey: "height")
aCoder.encode(name, forKey: "name")
}
required init?(coder aDecoder: NSCoder){
age = aDecoder.decodeObject(forKey: "age") as! Int **//error**
height = aDecoder.decodeObject(forKey: "height") as! Double
name = aDecoder.decodeObject(forKey: "name") as! String
super.init()
}
}
Then, I created an array of this class. I archived it to a plist using NSKeyedArchiver and everything was fine. However, when I tried to unarchive it I got an error involving unwrapping a optional which was nil. The error showed up in the Person class where marked. This is the code I used:
if let people = unarchive(){
print(people)
}
Here's the function for unarchiving:
func unarchive()->[Person]?{
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentDirectory = paths[0]
let path = documentDirectory.appending("ObjectData.plist")
let fileManager = FileManager.default
if(!fileManager.fileExists(atPath: path)){
if let bundlePath = Bundle.main.path(forResource: "ObjectData", ofType: "plist"){
do{
try fileManager.copyItem(atPath: bundlePath, toPath: path)
}catch{
print("problem copying")
}
}
}
if let objects = NSKeyedUnarchiver.unarchiveObject(withFile: path){
if let people = objects as? [Person]{
return people
}else{
return nil
}
}else{
return nil
}
}
Int and Double are not archived as objects. aDecoder.decodeObject(forKey: <key>) for them will always return nil, and using as! with nil will crash your app.
So, instead use this:
aDecoder.decodeInteger(forKey: "age")
aDecoder.decodeDouble(forKey: "height")
For name field you may keep your code.
You should use the proper decoding methods for Int and Double. You could paste the following code in a playground and test it :
import Foundation
class Person: NSObject, NSCoding{
var age:Int
var height: Double
var name: String
init(age:Int, height: Double, name:String){
self.age = age
self.height = height
self.name = name
}
func encode(with aCoder: NSCoder) {
aCoder.encode(age, forKey: "age")
aCoder.encode(height, forKey: "height")
aCoder.encode(name, forKey: "name")
}
required init?(coder aDecoder: NSCoder) {
age = aDecoder.decodeInteger(forKey: "age")
height = aDecoder.decodeDouble(forKey: "height")
name = aDecoder.decodeObject(forKey: "name") as! String
super.init()
}
}
let john = Person(age: 30, height: 170, name: "John")
let mary = Person(age: 25, height: 140, name: "Mary")
let guys = [john, mary]
let data = NSKeyedArchiver.archivedData(withRootObject: guys)
let people = NSKeyedUnarchiver.unarchiveObject(with: data)
dump (people)
Related
I'm having trouble archiving and/or unarchiving (not sure where the problem is, exactly) a set of custom classes from the iOS documents directory. The set is saved to disk (or at least it appears to be saved) because I can pull it from disk but I cannot unarchive it.
The model
final class BlockedUser: NSObject, NSSecureCoding {
static var supportsSecureCoding = true
let userId: String
let name: String
let date: Int
var timeIntervalFormatted: String?
init(userId: String, name: String, date: Int) {
self.userId = userId
self.name = name
self.date = date
}
required convenience init?(coder: NSCoder) {
guard let userId = coder.decodeObject(forKey: "userId") as? String,
let name = coder.decodeObject(forKey: "name") as? String,
let date = coder.decodeObject(forKey: "date") as? Int else {
return nil
}
self.init(userId: userId, name: name, date: date)
}
func encode(with coder: NSCoder) {
coder.encode(userId, forKey: "userId")
coder.encode(name, forKey: "name")
coder.encode(date, forKey: "date")
}
}
Writing to disk
let fm = FileManager.default
let dox = fm.urls(for: .documentDirectory, in: .userDomainMask)[0]
let dir = dox.appendingPathComponent("master.properties", isDirectory: true)
do {
let userData: [URL: Any] = [
/* Everything else in this dictionary is a primitive type (string, bool, etc.)
and reads and writes without problem from disk. The only thing I cannot
get to work is the entry below (the set of custom classes). */
dir.appendingPathComponent("blockedUsers", isDirectory: false): blockedUsers // of type Set<BlockedUser>
]
for entry in userData {
let data = try NSKeyedArchiver.archivedData(withRootObject: entry.value, requiringSecureCoding: true)
try data.write(to: entry.key, options: [.atomic])
}
} catch {
print(error)
}
Reading from disk
if let onDisk = try? Data(contentsOf: dir.appendingPathComponent("blockedUsers", isDirectory: false)) {
if let blockedUsers = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(onDisk) as? Set<BlockedUser> {
print("success")
} else {
print("file found but cannot unarchive") // where I'm currently at
}
} else {
print("file not found")
}
The problem is that you are trying to decode an object instead of decoding an integer. Check this post. Try like this:
class BlockedUser: NSObject, NSSecureCoding {
static var supportsSecureCoding = true
let userId, name: String
let date: Int
var timeIntervalFormatted: String?
init(userId: String, name: String, date: Int) {
self.userId = userId
self.name = name
self.date = date
}
func encode(with coder: NSCoder) {
coder.encode(userId, forKey: "userId")
coder.encode(name, forKey: "name")
coder.encode(date, forKey: "date")
coder.encode(timeIntervalFormatted, forKey: "timeIntervalFormatted")
}
required init?(coder: NSCoder) {
userId = coder.decodeObject(forKey: "userId") as? String ?? ""
name = coder.decodeObject(forKey: "name") as? String ?? ""
date = coder.decodeInteger(forKey: "date")
timeIntervalFormatted = coder.decodeObject(forKey: "timeIntervalFormatted") as? String
}
}
I have two model swift files under below.
// Item.swift
import UIKit
class Item: NSObject, NSCoding {
var name: String
var valueInDollars: Int
var serialNumber: String?
let dateCreated: Date
let itemKey: String
func encode(with aCoder: NSCoder) {
aCoder.encode(name, forKey: "name")
aCoder.encode(dateCreated, forKey: "dateCreated")
aCoder.encode(itemKey, forKey: "itemKey")
aCoder.encode(serialNumber, forKey: "serialNumber")
aCoder.encode(valueInDollars, forKey: "valueInDollars")
}
required init(coder aDecoder: NSCoder) {
name = aDecoder.decodeObject(forKey: "name") as! String
dateCreated = aDecoder.decodeObject(forKey: "dateCreated") as! Date
itemKey = aDecoder.decodeObject(forKey: "itemKey") as! String
serialNumber = aDecoder.decodeObject(forKey: "serialNumber") as! String?
valueInDollars = aDecoder.decodeInteger(forKey: "valueInDollars")
super.init()
}
}
// ItemStore.swift
import UIKit
class ItemStore {
var allItems = [Item]()
let itemArchiveURL: URL = {
let documentsDirectories =
FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentDirectory = documentsDirectories.first!
return documentDirectory.appendingPathComponent("items.archive")
}()
func saveChanges() -> Bool {
print("Saving items to: \(itemArchiveURL.path)")
return NSKeyedArchiver.archiveRootObject(allItems, toFile: itemArchiveURL.path)
}
}
These two model files confirming to NSCoding protocol and using archiveRootObject to archive the data.
But the archiveRootObject is deprecated, and the NSCoding is not as safe as the NSSecureCoding, how can I tweak the code to adjust all of these?
You can rewrite you saveChanges function to something like this:
func saveChanges() -> Bool {
print("Saving items to: \(itemArchiveURL.path)")
do {
let data = try NSKeyedArchiver.archivedData(withRootObject: allItems, requiringSecureCoding: false)
try data.write(to: itemArchiveURL)
}
catch {
print("Error archiving data: \(error)")
return false
}
return true
}
I was going to use Core Data but thought this was easier but can't find anything online about how to store an array of Cell class as a variable in the Cell class itself.
class Cell: NSObject, NSCoding {
var name:String!
var tag:String?
var more:String?
var amount:String!
var balance:String!
var subCells:[Cell]?//THIS IS THE ONE I'M STUGGLING WITH
override init() {
super.init()
}
init(name: String, tag: String, more: String, amount: String, balance: String, subCells: [Cell] = [Cell]()) {
super.init()
self.name = name
self.tag = tag
self.more = more
self.amount = amount
self.balance = balance
self.subCells = subCells//THIS
}
required init(coder decoder: NSCoder) {
self.name = decoder.decodeObject(forKey: "name") as! String
self.tag = decoder.decodeObject(forKey: "tag") as? String
self.more = decoder.decodeObject(forKey: "more") as? String
self.amount = decoder.decodeObject(forKey: "amount") as! String
self.balance = decoder.decodeObject(forKey: "balance") as! String
self.subCells = decoder.decodeObject(forKey: "subCells") as? [Cell]//THIS
}
func initWithCoder(decoder: NSCoder) -> Cell{
self.name = decoder.decodeObject(forKey: "name") as! String
self.tag = decoder.decodeObject(forKey: "tag") as? String
self.more = decoder.decodeObject(forKey: "more") as? String
self.amount = decoder.decodeObject(forKey: "amount") as! String
self.balance = decoder.decodeObject(forKey: "balance") as! String
self.subCells = decoder.decodeObject(forKey: "subCells") as? [Cell]//THIS
return self
}
func encode(with aCoder: NSCoder) {
aCoder.encode(self.name, forKey: "name")
aCoder.encode(self.tag, forKey: "tag")
aCoder.encode(self.more, forKey: "more")
aCoder.encode(self.amount, forKey: "amount")
aCoder.encode(self.balance, forKey: "balance")
aCoder.encode(self.subCells, forKey: "subCell")//THIS
}
}
Am I forced to use core data to solve this or can I encode the array before encoding the whole class in the viewdidload()
ok i figured it out
required init(coder decoder: NSCoder) {
self.name = decoder.decodeObject(forKey: "name") as! String
self.tag = decoder.decodeObject(forKey: "tag") as? String
self.more = decoder.decodeObject(forKey: "more") as? String
self.amount = decoder.decodeObject(forKey: "amount") as! String
self.balance = decoder.decodeObject(forKey: "balance") as! String
self.subCells = NSKeyedUnarchiver.unarchiveObject(with: (decoder.decodeObject(forKey: "subCells") as! NSData) as Data) as? [Cell]
}
func encode(with aCoder: NSCoder) {
aCoder.encode(self.name, forKey: "name")
aCoder.encode(self.tag, forKey: "tag")
aCoder.encode(self.more, forKey: "more")
aCoder.encode(self.amount, forKey: "amount")
aCoder.encode(self.balance, forKey: "balance")
aCoder.encode(NSKeyedArchiver.archivedData(withRootObject: self.subCells), forKey: "subCells")
}
I have a model class User that I want to save in UserDefaults
import UIKit
class User: NSObject {
var name:String!
var email:String!
var userId:String!
var phone:String!
var admin_status:String!
var social_code:String!
var token:String!
var otp:String!
var forget_otp:String!
var p_img:String!
var created:String!
var status:String!
static var currentUser:User = User()
override init() {
super.init()
}
required init(coder aDecoder: NSCoder) {
self.name = aDecoder.decodeObject(forKey: "name") as! String
self.email = aDecoder.decodeObject(forKey: "email") as! String
self.userId = aDecoder.decodeObject(forKey: "userId") as! String
self.phone = aDecoder.decodeObject(forKey: "phone") as! String
self.admin_status = aDecoder.decodeObject(forKey: "admin_status") as! String
self.social_code = aDecoder.decodeObject(forKey: "social_code") as! String
self.token = aDecoder.decodeObject(forKey: "token") as! String
self.otp = aDecoder.decodeObject(forKey: "otp") as! String
self.forget_otp = aDecoder.decodeObject(forKey: "forget_otp") as! String
self.p_img = aDecoder.decodeObject(forKey: "p_img") as! String
self.created = aDecoder.decodeObject(forKey: "created") as! String
self.status = aDecoder.decodeObject(forKey: "status") as! String
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encode(name, forKey: "name")
aCoder.encode(email, forKey: "email")
aCoder.encode(userId, forKey: "userId")
aCoder.encode(phone, forKey: "phone")
aCoder.encode(admin_status, forKey: "admin_status")
aCoder.encode(social_code, forKey: "social_code")
aCoder.encode(token, forKey: "token")
aCoder.encode(otp, forKey: "otp")
aCoder.encode(forget_otp, forKey: "forget_otp")
aCoder.encode(p_img, forKey: "p_img")
aCoder.encode(created, forKey: "created")
aCoder.encode(status, forKey: "status")
}
}
Code to save and get from UserDefaults
class func setUserDefault(ObjectToSave : AnyObject? , KeyToSave : String)
{
let defaults = UserDefaults.standard
if (ObjectToSave != nil)
{
defaults.set(ObjectToSave, forKey: KeyToSave)
}
UserDefaults.standard.synchronize()
}
class func getUserDefault(KeyToReturnValye : String) -> AnyObject?
{
let defaults = UserDefaults.standard
if let name = defaults.value(forKey: KeyToReturnValye)
{
return name as AnyObject
}
return nil
}
Saving
let user:User = User()
user.name = json["data"]["first_name"].string
user.email = json["data"]["email"].string
user.phone = json["data"]["phone"].string
user.social_code = json["data"]["social_code"].string
user.admin_status = json["data"]["admin_status"].string
user.otp = json["data"]["otp"].string
user.forget_otp = json["data"]["forget_otp"].string
user.p_img = json["data"]["p_img"].string
user.status = json["data"]["status"].string
user.userId = json["data"]["id"].string
user.created = json["data"]["created"].string
Utilities.setUserDefault(ObjectToSave: user, KeyToSave: "user")
I also tried this
let encodedData = NSKeyedArchiver.archivedData(withRootObject: user)
UserDefaults.standard.set(encodedData, forKey: "User")
but it crashes because of static var currentUser:User = User()
how to fix this ?
As for NSCoding: you have wrong method signature. Change
func encodeWithCoder(aCoder: NSCoder)
to
func encode(with aCoder: NSCoder)
Also, your object should explicitly conform to the NSCoding protocol:
class User: NSObject, NSCoding {...}
As for UserDefaults, you can not store custom objects in the UserDefaults, only NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary (or similar types in Swift), see documentation
Have you tried by replacing your this line,
static var currentUser:User = User()
with this line,
static var currentUser:User!
I feel as if I am doing things correctly, but I am getting an error at the end of my data conversion and retrieval. Please see the code below:
class Task:NSObject, NSCoding {
var name:String
var notes:String
var date:NSDate
var taskCompleted:Bool
init(name:String, notes:String,date:NSDate, taskCompleted:Bool){
self.name = name
self.notes = notes
self.date = date
self.taskCompleted = taskCompleted
}
required init(coder decoder: NSCoder){
self.name = (decoder.decodeObjectForKey("name") as! String?)!
self.notes = (decoder.decodeObjectForKey("notes") as! String?)!
self.date = (decoder.decodeObjectForKey("date") as! NSDate?)!
self.taskCompleted = (decoder.decodeObjectForKey("taskCompleted") as! Bool?)!
}
func encodeWithCoder(coder: NSCoder) {
coder.encodeObject(self.name, forKey: "name")
coder.encodeObject(self.notes, forKey: "notes")
coder.encodeObject(self.date, forKey: "date")
coder.encodeObject(self.taskCompleted, forKey: "taskCompleted")
}
}
I then save and get the data as follows:
let nowData = NSKeyedArchiver.archivedDataWithRootObject([nowTasks])
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(nowData, forKey: "nowData")
let loadedData = defaults.dataForKey("nowData")
let loadedArray = NSKeyedUnarchiver.unarchiveObjectWithData(loadedData!) as! [Task]
When I call print(loadedArray.first) I get the error: NSArray element failed to match the Swift Array Element type
Looks like your code should work fine even with some really weird forced casting going on in your decoder method. Try like this:
class Task: NSObject, NSCoding {
var name = String()
var notes = String()
var date: NSDate
var taskCompleted: Bool
init(name: String, notes: String, date: NSDate, taskCompleted: Bool){
self.name = name
self.notes = notes
self.date = date
self.taskCompleted = taskCompleted
}
required init(coder decoder: NSCoder){
self.name = decoder.decodeObjectForKey("name") as! String
self.notes = decoder.decodeObjectForKey("notes") as! String
self.date = decoder.decodeObjectForKey("date") as! NSDate
self.taskCompleted = decoder.decodeBoolForKey("taskCompleted")
}
func encodeWithCoder(coder: NSCoder) {
coder.encodeObject(name, forKey: "name")
coder.encodeObject(notes, forKey: "notes")
coder.encodeObject(date, forKey: "date")
coder.encodeBool(taskCompleted, forKey: "taskCompleted")
}
}
Testing with plist files:
let task1 = Task(name: "task1", notes: "note a", date: NSDate(), taskCompleted: false)
let task2 = Task(name: "task2", notes: "note b", date: NSDate(), taskCompleted: true)
let documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!
let fileURL = documentsDirectory.URLByAppendingPathComponent("data.plist")
if let filePath = fileURL.path {
NSKeyedArchiver.archiveRootObject([task1,task2], toFile: filePath)
if let loadedArray = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as? [Task] {
print(loadedArray.count)
print(loadedArray.first?.name ?? "")
print(loadedArray.first?.notes ?? "")
print(loadedArray.first!.date )
print(loadedArray.first!.taskCompleted)
print(loadedArray.last?.name ?? "")
print(loadedArray.last?.notes ?? "")
print(loadedArray.last!.date )
print(loadedArray.last!.taskCompleted)
}
}