1

I refactored some code into its own Swift Package. Part of it is a table view cell as .xib file, which I put in a Resources folder.

Two issues now came up which were not there before I refactored everything into its own Swift Package:

A) The constraints of the label in the cell are simply ignored, the text is at the top of the cell, while its supposed to be in the middle: enter image description here

It looks like this though in the simulator: enter image description here

B) Another issue which came up is that inside the class of that cell, the following code throws an error:

    @IBOutlet weak var resultTextLabel: UILabel! // this is the label
    override func awakeFromNib() {
        super.awakeFromNib()
        resultTextLabel.adjustsFontSizeToFitWidth = true // this line leads to the error
    }

'NSUnknownKeyException', reason: '[<UITableViewCell 0x156f06570> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key resultTextLabel.'

A simple refactoring into a Swift Package shouldn't break things like this.

Question: What's happening here? How can I make sure the constraints are working again? Why can't I adjust the textlabel font size option inside awakeFromNib anymore?

ffritz
  • 2,180
  • 1
  • 28
  • 64
  • Your custom cell is thought to be a `UITableViewCell`, not `YourCustomTableViewCell` Check the state of "Inherist Module" maybe in InterfaceBuilder where you set the class: https://stackoverflow.com/a/54556194/1801544 ? – Larme Feb 15 '21 at 09:46
  • @Larme The cell is connected to a custom class, that is `ResultCell: UITableViewCell`. Should be fine. – ffritz Feb 15 '21 at 09:47
  • When the error is `NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key resultTextLabel.'`, not the cell is not considered as a `ResultCell`. So I guess it's not loading the correct xib, or not understanding its class. Could you set the background color to orange, remove the crashing line, and see if the background is orange in your app? – Larme Feb 15 '21 at 09:48
  • @Larme When I untick "Inherit Module from Target" and under "Module" select the name of the Swift Package, it does not throw any errors, but the `resultTextLabel` inside `awakeFromNib` prints alternating `nil` and `Optional(UILabel...)`. So I have to do `if resultTextLabel != nil` to set a background color for the label, which does work. – ffritz Feb 15 '21 at 09:54
  • Did you update from main bundle to whatever bundle is for package? – Anton Tropashko Feb 15 '21 at 10:00
  • @AntonTropashko Where would that be specifically? Somewhere here? `let cell = resultTable.dequeueReusableCell(withIdentifier: "resultCell") as! ResultCell` – ffritz Feb 15 '21 at 10:07
  • @AntonTropashko I do register the cell properly with `.module`: `let nib = UINib(nibName: "ResultCell", bundle: .module) resultTable.register(nib, forCellReuseIdentifier: "resultCell")` – ffritz Feb 15 '21 at 10:50
  • check sister questions https://stackoverflow.com/questions/38496504/this-class-is-not-key-value-coding-compliant-for-the-key – Anton Tropashko Feb 15 '21 at 11:16
  • and this https://stackoverflow.com/questions/3088059/xcode-how-to-fix-nsunknownkeyexception-reason-this-class-is-not-key-valu – Anton Tropashko Feb 15 '21 at 11:17
  • also do be sure to check "module" under "class" for ResultCell in xib – Anton Tropashko Feb 15 '21 at 11:19
  • Already did check the outlet multiple times. Might be a bug. If I want to recreate the outlet via dragging, Xcode crashes. – ffritz Feb 15 '21 at 11:20
  • Well, xcode beta might have it fixed, though I doubt people do due diligence and report things like that. Did you consider to file a bugreport with apple at least on the xcode crash? There might not be any wrongdoing on your part in the original issue as well. – Anton Tropashko Feb 15 '21 at 11:22
  • @AntonTropashko I'm filing one, yes. Thanks for the help! – ffritz Feb 15 '21 at 11:52

1 Answers1

0

This is not your bug most likely. Apple bugreporter to the rescue.

Loading cells from xibs in your own frameworks works fine: it's odd that packages throw a wrench into works.

Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66