Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding in OSX: class is not key value coding-compliant for the key {name of binding var}

I was trying to follow instructions from Learning Swift book (creating note taking app) by B.A. Paris & Co, but faced with the following problem with binding. I am mostly practicing iOS programming, so binding concept is new for me.

Steps I made (tried both xcode 9 beta 5 and 8.3.3):

  1. Create OSX Cocoa App (not using storyboard, document based app – on, document extension “test”, don’t use core data)
  2. Add “var text = NSAttributedString()” to Document.swift
  3. Add a NSTextView to Document.xib
  4. In Bindings inspector of NSTextView setting “Attributed String” to File’s owner “self.text” (Model Key Path)

And I see exclamation mark with notion “Xcode cannot resolve the entered key path” Build is successful, but when I run it says “2017-09-03 22:17:40.739643+0200 test3[6017:424072] [<test3.Document 0x6180000c3410> valueForUndefinedKey:]: this class is not key value coding-compliant for the key text.”

I tried to control drag from Xib to Swift, it warns that “Xcode cannot locate the class Document in the current workspace”.

I tried to convert to workspace instead of proj, checked the file owner, checked the stackoverflow threads witch relate to the error – but they mostly concerned about some connection made by mistake or non actual connections (I can delete the connection, I know what connection is wrong, the question is how to make it right). So far could not find solution.

Thanks in advance

like image 802
Andrey Avatar asked Sep 03 '17 20:09

Andrey


1 Answers

You need to declare the text property with the @objc attribute to make it accessible via dynamic dispatch like Key-Value Coding.

Also, because you want modifications of the property to be observable via Key-Value Observing (for Bindings), you need to tell Swift to always dispatch modifications of it dynamically. So, you need to use the dynamic modifier on the declaration, too:

@objc dynamic var text = NSAttributedString()
like image 147
Ken Thomases Avatar answered Oct 21 '22 11:10

Ken Thomases