Jack tileman
Jack tileman

Reputation: 871

Failed to set (key_name) user defined inspected property . this class is not key value coding-compliant for the key

My code was working fine until we upgraded from Swift 3 to Swift 5.

We are using Storyboard (not xib). Have created a custom CollectionViewCell.

We gave tried Product -> Clean Build Folder many times.

We get the error at this statement.

    // Configure the cell
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: self.reuseIdentifier, for: indexPath as IndexPath) as! ActivitiesSelectMembersCell
    cell.setValue(member.member_element_id, forKeyPath: "member_element_id")       

Here is the error statement

 Failed to set (member_element_id) user defined inspected property on 
 (ChildOpenBarn.ActivitiesSelectMembersCell): 
 [<ChildOpenBarn.ActivitiesSelectMembersCell 0x17edc2a0> 
 setValue:forUndefinedKey:]: this class is not key value coding- 
 compliant for the key member_element_id.

Our CollectionViewController has this -

class ActivitiesSelectMembers: UICollectionViewController {


let reuseIdentifier: String = "ActivitiesSelectMembersCellId"
  required init?(coder aDecoder: NSCoder) {
       super.init(coder: aDecoder)
   }

override func awakeFromNib() {
   super.awakeFromNib()
   // Initialization code

    self.collectionView.dataSource = self
    self.collectionView.delegate = self
    // self.collectionView.register(UINib.init(nibName: "ActivitiesSelectMembersCell", bundle: nil), forCellWithReuseIdentifier: self.reuseIdentifier)

}

override func viewDidLoad() {
    super.viewDidLoad()

    self.collectionView?.register(UITableViewHeaderFooterView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "ActivitiesSelectMembersFooterView");

    self.collectionView?.dataSource = self
    self.collectionView?.delegate = self        

    member_list_from_dm = loadMemberList()!

 }

// Configure the cell
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    // let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)

    // Configure the cell
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: self.reuseIdentifier, for: indexPath as IndexPath) as! ActivitiesSelectMembersCell

    cell.setValue(member.member_element_id, forKeyPath: "member_element_id")
    return cell
 }

We have checked IB to make sure the following -

enter image description here

The UICollectionViewCell has this

class ActivitiesSelectMembersCell: UICollectionViewCell {

    @IBOutlet weak var ActivitiesSelectMemberImage: UIImageView!

    @IBOutlet weak var ActivitiesSelectMemberLabel: UILabel!    

    override func awakeFromNib() {
      super.awakeFromNib()
      // Initialization code             
    }

    // var member_element_id:String = ""
    var member_element_id:String! {
       didSet {

     }
    }

    required init?(coder aDecoder: NSCoder)  {
       super.init(coder: aDecoder)!
       // member_element_id = ""
    }

 }

Upvotes: 0

Views: 481

Answers (1)

jrturton
jrturton

Reputation: 119292

You need to individually mark properties as @objc to enable key-value coding. Prior to swift 4, any @objc class (like ones inheriting from NSObject) would assume that all properties were @objc. After, you had to mark them individually.

@objc var member_element_id:String!

See here for the swift evolution proposal that made this happen.

Upvotes: 1

Related Questions