korum
korum

Reputation: 170

How to fix 'unrecognized selector sent to instance' in Swift 4

I'm trying to make a simple to-do list app using UserDefaults to store tasks. My program was working when I was using a string array to store, but as soon as I switched to storing my own objects, the issues started. The code might have some redundancy but I'm still learning how to store data locally on devices.

I just need to know why this error is occurring and how I should go about fixing it.

I've added spots in my code that I think the problem lies within, but I'm going to add all my code just in case it makes it clearer.

SPOT 1: I added objc(ABCItems) here based on what the console told me.

SPOT 2: I think there's an issue here because the code is failing around this point(The two print statements I have after never run).

import UIKit

class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{

//SPOT 1

@objc(ABCItems)class Items: NSCoder{

    var title : String!
    var completed : Bool!

    required init(coder decoder: NSCoder) {
        self.title = decoder.decodeObject(forKey: "title") as? String
        self.completed = decoder.decodeObject(forKey: "completed") as? Bool
    }

    init(title: String, completed: Bool) {
        self.title = title
        self.completed = completed
    }

    func encodeWithCoder(coder: NSCoder) {
        if let title = title { coder.encode(title, forKey: "title") }
        if let completed = completed { coder.encode(completed, forKey: "completed") }
    }
}

let defaults = UserDefaults.standard;
var list = [Items]();
@IBOutlet weak var tableList: UITableView!

@IBAction func addItem(_ sender: Any) {
    let alert = UIAlertController(title: "Add an item", message: "", preferredStyle: .alert);
    alert.addTextField(configurationHandler: { (textField) in
        textField.placeholder = "Enter Task";
    })
    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak alert] (_) in
        let textField = alert?.textFields![0];
        print("Text field: \(textField?.text ?? "")")
        if (textField?.text?.count)! > 0 {
            let item = Items(title: (textField?.text)!, completed: true);
            self.list.append(item);

            //SPOT 2

            let itemData = try! NSKeyedArchiver.archivedData(withRootObject: self.list, requiringSecureCoding: false);
            print("PASS 1");
            self.defaults.set(itemData, forKey: "list");
            print("PASS 2");
            self.tableList.reloadData();
        }
    }))
    self.present(alert, animated: true, completion: nil);
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return list.count;
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "cell");
    cell.textLabel?.text = (list[indexPath.row] as AnyObject).title;
    cell.backgroundColor = UIColor (red: 0.18, green: 0.71, blue: 1.0, alpha: 1.0);
    return cell;
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == UITableViewCell.EditingStyle.delete {
        self.list.remove(at: indexPath.row);
        tableList.reloadData();
    }

}
override func viewDidLoad() {
    super.viewDidLoad()
    let itemDataRet = defaults.object(forKey: "list") as? NSData

    if let itemDataRet = itemDataRet {
        list = try! NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(itemDataRet as Data) as! [Items]
    }
}
}

This is the output I get: "-[ABCItems encodeWithCoder:]: unrecognized selector sent to instance 0x2829159e0"

Upvotes: 0

Views: 1365

Answers (1)

vadian
vadian

Reputation: 285039

A class which adopts NSCoding(not NSCoder) must be a subclass of NSObject.

class Items: NSObject, NSCoding { ...

But in Swift 4 it's highly recommended to drop the ObjC runtime and use lightweight Codable protocol to serialize custom structs or classes to Property List or JSON.


The class can be reduced to a struct

struct Item : Codable {
    var title : String
    var completed : Bool
}

Load data :

guard let data = UserDefaults.standard.data(forKey: "list") else {
    self.list = []
    return
}
do {
   self.list = try JSONDecoder().decode([Item].self, from: data)
} catch {
   print(error)
   self.list = []
}

Save data :

do {
    let itemData = try JSONEncoder().encode(list)
    UserDefaults.standard.set(itemData, forKey: "list")
} catch {
    print(error)
}

A few notes:

  • Never declare properties as implicit unwrapped optional which are initialized in init methods. Either use regular optional ? or non-optional.
  • This is Swift: No trailing semicolons.
  • Structs and classes are supposed to be named in singular form (Item).

Upvotes: 6

Related Questions