2

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)
}


        }
Community
  • 1
  • 1
Andy Dewan
  • 245
  • 1
  • 2
  • 18
  • You should show the code where these errors occur. – Marcus Adams Feb 05 '15 at 18:20
  • This is for Objective-C, but it explains the unrecognized selector exception: http://stackoverflow.com/questions/25853947/how-can-i-debug-unrecognized-selector-sent-to-instance-error – Hot Licks Feb 05 '15 at 18:23
  • You define `Items` as a class conforming to the `NSObject` protocol, this does not mean that it _is_ an NSObject. Indeed you define another type inside the `Items` definition. NSObjects can't have other classes defined inside their definitions ... as well as functions, structs, enum. Probably this is a source of your problem. – Matteo Piombo Feb 05 '15 at 18:58

1 Answers1

2

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.

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • I recreated the app from scratch, following the tutorial step by step and got the exact same errors. I deleted Xcode and am reinstalling it to see if the error is some kind of bug. – Andy Dewan Feb 05 '15 at 22:50
  • Feel free to compress your project folder and upload it somewhere (e.g. dropbox or wherever you want) and I can take a look, too. I bet it's going to be something simple that's evading us right now. But, I tried to reproduce your error through a variety of techniques, but wasn't able to. I bet if we take a look at the project we'll find it, though. – Rob Feb 05 '15 at 22:53
  • When I remove the custom class name from the IB for the View Controller the app launches, but the table view is empty, and I get the error of Unknown class SelfieTableView in Interface Builder file – Andy Dewan Feb 05 '15 at 23:25
  • 1
    OK, there were tons of little issues here (mostly in the storyboard and associated with that storyboard). See https://dl.dropboxusercontent.com/u/11739737/h2.zip (which now includes a README.md listing all the little changes I made). Anyway, I mainly tweaked it as I outlined there and it works fine. (Clearly I didn't touch that other scene in the project, but focused on that which you referenced in the question, above.) – Rob Feb 06 '15 at 01:54