Andy Dewan
Andy Dewan

Reputation: 245

Why is there an uncaught exception?

I am getting a few errors using Swift, and when I have tried the solutions listed on other Stack Overflow posts, I get additional errors. The first error I get is NSForwarding: warning: object 0x7dc4ca30 of class 'h2.Items' does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector -[h2.Items initWithCoder:]

I've then updated my code to add "NSObject" and when I do that I get the following error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[h2.Items initWithCoder:]: unrecognized selector sent to instance 0x79069250'

I'm not sure where to go from here. I have been following the tutorial on http://shrikar.com/blog/2015/01/17/uitableview-and-uitableviewcell-customization-in-swift/

and tried the solutions from Got Unrecognized selector -replacementObjectForKeyedArchiver: crash when implementing NSCoding in Swift

Does anyone have any additional suggestions?

My code is:

import CloudKit
import UIKit
import Foundation

class Items: NSObject  
{
    class Entry 
    {
        var filename : String
        init(fname : String)
        {
            self.filename = fname
        }
    }

    var pics = [
        Entry(fname: "circle.png")
    ]
}

Here is the code that is calling that class:

var items = Items()

override func viewDidLoad() {
    super.viewDidLoad()
}


override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return items.pics.count
}


override func tableView(tableVIew: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TableViewCell

    let entry = items.pics[indexPath.row]
    let image = UIImage(named: entry.filename)
    cell.anotherSelfie.image = image


    return cell
}

}

and here is the code for the cell:

class TableViewCell: UITableViewCell {

@IBOutlet var anotherSelfie: UIImageView!


override func awakeFromNib() {
    super.awakeFromNib()

}

override func setSelected(selected: Bool, animated: Bool) {
   super.setSelected(selected, animated: animated)
}


        }

Upvotes: 2

Views: 1094

Answers (1)

Rob
Rob

Reputation: 438232

There's nothing in this question that would result in the error you describe. I used your code, even without NSObject reference, and it works fine. The problem rests elsewhere.

There's got to be something else going on that you haven't shared with us (e.g. doing state restoration, calling NSKeyedArchiver yourself, perhaps accidentally hooked up Items reference to something in storyboard, etc.). But, whatever you're doing, it's trying to call init(coder aDecoder: NSCoder) (aka initWithCoder).

So, you have two alternatives:

  • You should identify what's triggering initWithCoder to be called for this Items object. From there, you can decide whether you really need to go through this effort or not.

  • If you concluded that you need initWithCoder (and possibly encodeWithCoder, too, depending up what's calling this), you could carry on and make this class NSCoding compliant by implementing

    init(coder decoder: NSCoder) { ... }
    func encodeWithCoder(_ encoder: NSCoder) { ... }
    

    See the Encoding and Decoding Objects in the Archives and Serializations Programming Guide. That's admitted written for Objective-C, but the idea is the same in Swift.

    By the way, if you concluded that you really needed to make Items compliant with NSCoding, then it's likely you'd have to do it for Entry, too.

But I'd only go through that effort of complying to NSCoding once I established what was requiring this, and confirm that this was truly needed for my app.

Quite frankly, unless you're engaging in archives or state restoration, I'd be quite surprised if you needed to go down that road. The tutorial you're following is not engaging in that and I see nothing here that would suggest it's necessary here.

If you're unable to find the unintended reference to Items that's triggering this initWithCoder, I might suggest starting the project from scratch and see if you can reproduce the problem. If you are able to reproduce the problem in a blank project, share with us the precise steps you followed to manifest this issue, as I'm unable to reproduce the problem you describe. We need a MCVE and I'm unable to reproduce the problem you describe with the code that has been provided thus far.

Upvotes: 2

Related Questions