I'm really new using the NSCoding functionality, I'm trying to create persistence of data of an attributedString, particularly a UIColor that is passed through a delegate. I haven't found a tutorial that encodes values that are not declared and initialized in the same class in which the NSCoding protocol is conformed.
I have the following code, which is the method that conforms to the protocol I created, and assigns the passed color value as an attribute to the attributedString.
func didSelectColorCell(color: UIColor) {
let coder = NSCoder.init()
color.encode(with: coder)
noteTextView.setAttributedValueAtSelectedTextRange(NSForegroundColorAttributeName, value: color)
}
The app crashes, and sends me a warning "cannot be sent to an abstract object of class NSCoder: Create a concrete instance!" I'm really lost on how to procede. I don't know how to adapt this tutorial http://nshipster.com/nscoding/ to my scenario.
Can someone please provide me guidance on how to order my ideas or how does NSCoding work with delegates? Any help would be appreciated :)
Define a class to implement the NSCoding protocol (Playground sample):
class ColorHelper: NSObject, NSCoding {
var color: UIColor?
init(color: UIColor) {
super.init()
self.color = color
}
required init(coder aDecoder: NSCoder) {
if let color = aDecoder.decodeObject(forKey: "color") as? UIColor {
self.color = color
}
}
func encode(with aCoder: NSCoder) {
aCoder.encode(color, forKey: "color")
}
func save(defaults key: String) -> Bool {
let defaults = UserDefaults.standard
let savedData = NSKeyedArchiver.archivedData(withRootObject: data)
defaults.set(savedData, forKey: key)
return defaults.synchronize()
}
convenience init?(defaults key: String) {
let defaults = UserDefaults.standard
if let data = defaults.object(forKey: key) as? Data,
let obj = NSKeyedUnarchiver.unarchiveObject(with: data) as? ColorHelper,
let color = obj.color {
self.init(color: color)
} else {
return nil
}
}
}
let data = ColorHelper(color: .red)
let savedData = NSKeyedArchiver.archivedData(withRootObject: data)
let obj = NSKeyedUnarchiver.unarchiveObject(with: savedData) as? ColorHelper
obj?.color
In your code:
func didSelectColorCell(color: UIColor) {
let helper = ColorHelper(color: color)
helper.save(defaults: "color")
noteTextView.setAttributedValueAtSelectedTextRange(NSForegroundColorAttributeName, value: color)
}
To read the saved data:
let color = ColorHelper(defaults: "color")?.color
Related
I am trying to save store a custom file using UserDefaults, but my app crashesh due to :
fatal error: unexpectedly found nil while unwrapping an Optional value
Here is my code , in my custom class I declare an empty array
class AppDefualts: NSObject {
var downloadedURLArray = [DownloadFile]() //Declare an empty array
override init() {
super.init()
downloadedURLArray = loadStoredURL()
}
//Store data
func addStored(file:DownloadFile) {
//Add URL to array and save it
downloadedURLArray.append(file)
let data = NSKeyedArchiver.archivedData(withRootObject: downloadedURLArray)
UserDefaults.standard.set(data, forKey: "storedURL")
}
//Load:
func loadStoredURL() -> Array<DownloadFile> {
let data = UserDefaults.standard.data(forKey: "storedURL")
downloadedURLArray = NSKeyedUnarchiver.unarchiveObject(with: data!) as? [DownloadFile] ?? [DownloadFile]()
return downloadedURLArray
}
Any help would be great
EDIT 1 :
I added NSCoding protocols :
func encode(with aCoder: NSCoder) {
aCoder.encode(downloadedURLArray, forKey: "storedURL")
}
required init?(coder aDecoder: NSCoder) {
downloadedURLArray = aDecoder.decodeObject(forKey: "storedURL") as! [DownloadFile]
}
now app crashes due to this :
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -encodeObject:forKey:
cannot be sent to an abstract object of class NSCoder: Create a
concrete instance!'
If you want to store the custom object in UserDefault then you have to do Encoding/Decoding. Your custom object suppose to confirm to the NSCoding Protocol.
Store into UserDefault : You have to convert you object in to NSData and store them into UserDefault by using NSKeyedArchiver
Get from UserDefault : Get the NSData from USerDefault and convert into Custom object by using NSKeyedUnArchiver
Refer the link to convert custom object into NSData and vice versa
Example: custom object which confirms NSCoding protocol
class Book: NSObject, NSCoding {
var title: String
var author: String
var pageCount: Int
var categories: [String]
var available: Bool
// Memberwise initializer
init(title: String, author: String, pageCount: Int, categories: [String], available: Bool) {
self.title = title
self.author = author
self.pageCount = pageCount
self.categories = categories
self.available = available
}
// MARK: NSCoding
required convenience init?(coder decoder: NSCoder) {
guard let title = decoder.decodeObjectForKey("title") as? String,
let author = decoder.decodeObjectForKey("author") as? String,
let categories = decoder.decodeObjectForKey("categories") as? [String]
else { return nil }
self.init(
title: title,
author: author,
pageCount: decoder.decodeIntegerForKey("pageCount"),
categories: categories,
available: decoder.decodeBoolForKey("available")
)
}
func encodeWithCoder(coder: NSCoder) {
coder.encodeObject(self.title, forKey: "title")
coder.encodeObject(self.author, forKey: "author")
coder.encodeInt(Int32(self.pageCount), forKey: "pageCount")
coder.encodeObject(self.categories, forKey: "categories")
coder.encodeBool(self.available, forKey: "available")
}
}
NSCoding requires init(coder:), but there is also the optional version of this method init?(coder:).
What exactly should one do if this returns nil? Is this even an issue?
Say you are initializing a large hierarchy of objects with init(coder:), with each object's child objects themselves being initialized using init?(coder:). If somewhere along the way one of those objects is nil, wouldn't the app crash? The parent object is not expecting a nil child.
What does it even mean to "init nil"?
class Parent: NSCoding {
var children: [Child]
required init?(coder aDecoder: NSCoder) {
guard let children = aDecoder.decodeObject(forKey: "children") as? [Child] else { return nil }
self.children = children
}
}
class Child: NSCoding {
var name: String
required init?(coder aDecoder: NSCoder) {
guard let name = aDecoder.decodeObject(forKey: "name") as? String else { return nil }
self.name = name
}
}
One strategy would be to simply return a new instance rather than simply returning nil. The data would be lost, but it the app would run.
You'd better not return nil.
As my test in Xcode 8.3.2 (8E2002), return nil in init(coder:) cause NSKeyedUnarchiver.unarchiveObject crash or return unexpected result.
Prepare a class which encode wrong data type for "test2":
class MyClass: NSObject, NSCoding {
var x: String
init(_ x: String) {
self.x = x
}
required init?(coder aDecoder: NSCoder) {
guard let x = aDecoder.decodeObject(forKey: "x") as? String else {
return nil
}
self.x = x
}
func encode(with aCoder: NSCoder) {
if x == "test2" {
aCoder.encode(Int(4), forKey: "x")
} else {
aCoder.encode(x, forKey: "x")
}
}
}
TestCaseA: archive a dictionary which contains above MyClass, then unarchive.
Result: crash on NSKeyedUnarchiver.unarchiveObject.
let encodedData = NSKeyedArchiver.archivedData(withRootObject: [
"k1":MyClass("test1"),
"k2":MyClass("test2"),
"k3":"normal things"
])
UserDefaults.standard.set(encodedData, forKey: "xx")
if let data = UserDefaults.standard.data(forKey: "xx"),
let _data = NSKeyedUnarchiver.unarchiveObject(with: data) {
if let dict = _data as? [String:Any] {
debugPrint(dict.count)
}
}
TestCaseB: archive an array which contains above MyClass, then unarchive.
Result: return an empty array (but expected is an array with 1 element)
let encodedData = NSKeyedArchiver.archivedData(withRootObject: [
MyClass("test1"),
MyClass("test2")
])
UserDefaults.standard.set(encodedData, forKey: "xx")
if let data = UserDefaults.standard.data(forKey: "xx"),
let _data = NSKeyedUnarchiver.unarchiveObject(with: data) {
if let dict = _data as? [Any] {
debugPrint(dict.count)
}
}
Current Format
Optional(posterPrint.Frame(name: "RIBBA", productNumber: "303.016.24", productSize: (63.0, 93.0), pictureSize: (61.0, 91.0), pictureWithMatSize: (50.0, 70.0), frameColor: [UIDeviceRGBColorSpace 0.92549 0.92549 0.92549 1], matColor: [UIDeviceRGBColorSpace 0.92549 0.92549 0.92549 1]))
I want to add this one to array and convert any object. I tried a lot in this to stored in NSkeyedArchiver. I read somewhere tuples are not possible to store in NSKeyedArchiever. Anyone help in this
Thanks in advance .
To use NSKeyedArchiver, you make your object conform to NSCoding. And if you have tuples, you can encode/decode the elements separately:
class MyObject: NSObject, NSCoding {
var id: Int
var size: (Float, Float)
init(id: Int, size: (Float, Float)) {
self.id = id
self.size = size
super.init()
}
required init?(coder aDecoder: NSCoder) {
guard let id = aDecoder.decodeObjectForKey("id") as? Int else { return nil }
guard let size0 = aDecoder.decodeObjectForKey("size.0") as? Float else { return nil }
guard let size1 = aDecoder.decodeObjectForKey("size.1") as? Float else { return nil }
self.id = id
self.size = (size0, size1)
super.init()
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(id, forKey: "id")
aCoder.encodeObject(size.0, forKey: "size.0")
aCoder.encodeObject(size.1, forKey: "size.1")
}
}
You mention arrays, so I'm not sure if you're talking about an array of your custom objects, but now that your object conforms to NSCoding, you can now archive and unarchive arrays of these objects with no further effort.
Likewise, if you have some other object that has a MyObject property, you can apply NSCoding to that, and it can take advantage of the fact that MyObject already conforms to NSCoding:
class ParentObject: NSObject, NSCoding {
var name: String
var value: Int
var child: MyObject
init(name: String, value: Int, child: MyObject) {
self.name = name
self.value = value
self.child = child
super.init()
}
required init?(coder aDecoder: NSCoder) {
name = aDecoder.decodeObjectForKey("name") as! String
value = aDecoder.decodeObjectForKey("value") as! Int
child = aDecoder.decodeObjectForKey("child") as! MyObject
super.init()
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(name, forKey: "name")
aCoder.encodeObject(value, forKey: "value")
aCoder.encodeObject(child, forKey: "child")
}
}
I am trying to use NSUserDefaults to save an array in to my app's core data. I thought it would be good to use NSUserDefaults but the problem is that wherever I put the code that creates the default it throws up the SIGABRT error.
Here is the code that creates the default:
let levelArrayDefault = NSUserDefaults.standardUserDefaults()
levelArrayDefault.setValue(levelsArray, forKey: "levelsArray")
levelArrayDefault.synchronize()
levelsArray is an array of List objects:
class List: NSObject, NSCoding {
// MARK: Properties
var name: String
var AnswersArray = [Answer]()
init?(name: String) {
// Initialize stored properties.
self.name = name
if name.isEmpty {
return nil
}
}
required init(coder decoder: NSCoder){
self.AnswersArray = (decoder.decodeObjectForKey("AA") as? [Answer])!
self.name = (decoder.decodeObjectForKey("name") as? String)!
}
func encodeWithCoder(coder: NSCoder) {
if let AnswersArray = AnswersArray { coder.encodeObject(AnswersArray, forKey: "AA") }
if let name = name { coder.encodeObject(name, forKey: "name") }
}
}
class Answer: NSObject, NSCoding {
var EnglishAnswer: String = ""
var ChineseAnswer: String = ""
init(newEng: String, newChi: String){
self.EnglishAnswer = newEng
self.ChineseAnswer = newChi
}
required init(coder decoder: NSCoder){
self.EnglishAnswer = (decoder.decodeObjectForKey("EnglishAnswer") as? String)!
self.ChineseAnswer = (decoder.decodeObjectForKey("ChineseAnswer") as? String)!
}
func encodeWithCoder(coder: NSCoder) {
if let EnglishAnswer = EnglishAnswer { coder.encodeObject(EnglishAnswer, forKey: "EnglishAnswer") }
if let ChineseAnswer = ChineseAnswer { coder.encodeObject(ChineseAnswer, forKey: "ChineseAnswer") }
}
}
How can I stop SIGABRT from popping up and get the array to be stored.
Help would be much appreciated.
You need to convert it to NSData using NSKeyedArchiver before storing it to NSUserDefaults, try like this:
update: Xcode 11.4 • Swift 5.2 or later
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let list = List(name: "Student")
list.answers = [Answer(english: "english answer", chinese: "中文回答")]
let data = (try? NSKeyedArchiver.archivedData(withRootObject: [list], requiringSecureCoding: false)) ?? Data()
UserDefaults.standard.set(data, forKey: "listData")
guard
let loadedData = UserDefaults.standard.data(forKey: "listData"),
let loadedArray = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(loadedData) as? [List]
else { return }
print(loadedData.count)
print(loadedArray.first ?? "none")
print(loadedArray.first?.name ?? "no name")
print(loadedArray.first?.answers.first?.english ?? "no english")
print(loadedArray.first?.answers.first?.chinese ?? "no chinese")
}
}
class Answer: NSObject, NSCoding {
let english: String
let chinese: String
init(english: String, chinese: String) {
self.english = english
self.chinese = chinese
}
required init(coder decoder: NSCoder) {
self.english = decoder.decodeString(forKey: "english")
self.chinese = decoder.decodeString(forKey: "chinese")
}
func encode(with coder: NSCoder) {
coder.encode(english, forKey: "english")
coder.encode(chinese, forKey: "chinese")
}
}
class List: NSObject, NSCoding {
let name: String
fileprivate var data = Data()
var answers: [Answer] {
get {
(try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data)) as? [Answer] ?? []
}
set {
data = (try? NSKeyedArchiver.archivedData(withRootObject: newValue, requiringSecureCoding: false)) ?? Data()
}
}
init(name: String) {
self.name = name
}
required init(coder decoder: NSCoder) {
self.data = decoder.decodeData(forKey: "answersData")
self.name = decoder.decodeString(forKey: "name")
}
func encode(with coder: NSCoder) {
coder.encode(data, forKey: "answersData")
coder.encode(name, forKey: "name")
}
}
extension NSCoder {
func decodeString(forKey key: String) -> String {
return decodeObject(forKey: key) as? String ?? ""
}
func decodeData(forKey key: String) -> Data {
return decodeObject(forKey: key) as? Data ?? Data()
}
}
If you want to save your custom object in NSUserDefaults, it's not enough to make your class NSCoding-compliant -- you have to actually encode the data into an NSData object. This is a common mistake -- see my answer to another question for a similar situation.
So, you've added NSCoding to your Answer and List classes. That's a good start. Before you continue, you should verify that you've got that step right by using a NSKeyedArchiver to encode an example of a List object containing a few Answer objects into an instance of NSData, and then use NSKeyedUnarchiver to decode that data object back into your List. Verify that everything that you care about completes the round trip with no problems. This would be an excellent place to use Xcode's testing facility -- you could write a unit test that does exactly what I've described.
Once you know you've got the NSCoding stuff right, you should modify your code so that it encodes your List as NSData and stores the resulting data object in NSUserDefaults using the -setObject:forKey: method.
I am trying to use NSUserDefaults to save an array in to my app's core data. I thought it would be good to use NSUserDefaults but the problem is that wherever I put the code that creates the default it throws up the SIGABRT error.
Here is the code that creates the default:
let levelArrayDefault = NSUserDefaults.standardUserDefaults()
levelArrayDefault.setValue(levelsArray, forKey: "levelsArray")
levelArrayDefault.synchronize()
levelsArray is an array of List objects:
class List: NSObject, NSCoding {
// MARK: Properties
var name: String
var AnswersArray = [Answer]()
init?(name: String) {
// Initialize stored properties.
self.name = name
if name.isEmpty {
return nil
}
}
required init(coder decoder: NSCoder){
self.AnswersArray = (decoder.decodeObjectForKey("AA") as? [Answer])!
self.name = (decoder.decodeObjectForKey("name") as? String)!
}
func encodeWithCoder(coder: NSCoder) {
if let AnswersArray = AnswersArray { coder.encodeObject(AnswersArray, forKey: "AA") }
if let name = name { coder.encodeObject(name, forKey: "name") }
}
}
class Answer: NSObject, NSCoding {
var EnglishAnswer: String = ""
var ChineseAnswer: String = ""
init(newEng: String, newChi: String){
self.EnglishAnswer = newEng
self.ChineseAnswer = newChi
}
required init(coder decoder: NSCoder){
self.EnglishAnswer = (decoder.decodeObjectForKey("EnglishAnswer") as? String)!
self.ChineseAnswer = (decoder.decodeObjectForKey("ChineseAnswer") as? String)!
}
func encodeWithCoder(coder: NSCoder) {
if let EnglishAnswer = EnglishAnswer { coder.encodeObject(EnglishAnswer, forKey: "EnglishAnswer") }
if let ChineseAnswer = ChineseAnswer { coder.encodeObject(ChineseAnswer, forKey: "ChineseAnswer") }
}
}
How can I stop SIGABRT from popping up and get the array to be stored.
Help would be much appreciated.
You need to convert it to NSData using NSKeyedArchiver before storing it to NSUserDefaults, try like this:
update: Xcode 11.4 • Swift 5.2 or later
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let list = List(name: "Student")
list.answers = [Answer(english: "english answer", chinese: "中文回答")]
let data = (try? NSKeyedArchiver.archivedData(withRootObject: [list], requiringSecureCoding: false)) ?? Data()
UserDefaults.standard.set(data, forKey: "listData")
guard
let loadedData = UserDefaults.standard.data(forKey: "listData"),
let loadedArray = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(loadedData) as? [List]
else { return }
print(loadedData.count)
print(loadedArray.first ?? "none")
print(loadedArray.first?.name ?? "no name")
print(loadedArray.first?.answers.first?.english ?? "no english")
print(loadedArray.first?.answers.first?.chinese ?? "no chinese")
}
}
class Answer: NSObject, NSCoding {
let english: String
let chinese: String
init(english: String, chinese: String) {
self.english = english
self.chinese = chinese
}
required init(coder decoder: NSCoder) {
self.english = decoder.decodeString(forKey: "english")
self.chinese = decoder.decodeString(forKey: "chinese")
}
func encode(with coder: NSCoder) {
coder.encode(english, forKey: "english")
coder.encode(chinese, forKey: "chinese")
}
}
class List: NSObject, NSCoding {
let name: String
fileprivate var data = Data()
var answers: [Answer] {
get {
(try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data)) as? [Answer] ?? []
}
set {
data = (try? NSKeyedArchiver.archivedData(withRootObject: newValue, requiringSecureCoding: false)) ?? Data()
}
}
init(name: String) {
self.name = name
}
required init(coder decoder: NSCoder) {
self.data = decoder.decodeData(forKey: "answersData")
self.name = decoder.decodeString(forKey: "name")
}
func encode(with coder: NSCoder) {
coder.encode(data, forKey: "answersData")
coder.encode(name, forKey: "name")
}
}
extension NSCoder {
func decodeString(forKey key: String) -> String {
return decodeObject(forKey: key) as? String ?? ""
}
func decodeData(forKey key: String) -> Data {
return decodeObject(forKey: key) as? Data ?? Data()
}
}
If you want to save your custom object in NSUserDefaults, it's not enough to make your class NSCoding-compliant -- you have to actually encode the data into an NSData object. This is a common mistake -- see my answer to another question for a similar situation.
So, you've added NSCoding to your Answer and List classes. That's a good start. Before you continue, you should verify that you've got that step right by using a NSKeyedArchiver to encode an example of a List object containing a few Answer objects into an instance of NSData, and then use NSKeyedUnarchiver to decode that data object back into your List. Verify that everything that you care about completes the round trip with no problems. This would be an excellent place to use Xcode's testing facility -- you could write a unit test that does exactly what I've described.
Once you know you've got the NSCoding stuff right, you should modify your code so that it encodes your List as NSData and stores the resulting data object in NSUserDefaults using the -setObject:forKey: method.