@IBDesignable - @IBInspectable, class not key value coding-compliant
Asked Answered
K

5

10

I'm suddenly having a strange and reproducible error when using an @IBDesignable class, @IBInspectable properties are giving the following warning:

Main.storyboard: warning: IB Designables: Ignoring user defined runtime attribute for key path "test" on instance of "UIButton". Hit an exception when attempting to set its value: [ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key test.

To be clear this warning is only a problem in Xcode, it packages fine. Still annoying!

Steps to reproduce

  1. Create new project
  2. Create a custom @IBDesignable UIButton subclass, with an @IBInspectable String property:

    import UIKit
    
    @IBDesignable
    class TempButton: UIButton {
        @IBInspectable var test:String = ""
    }
    
    1. Drag a Button to a View Controller in the storyboard, and give it the custom class "TempButton" in the Identity Inspector.
    2. Now the 'Test' attribute should appear in the Attributes Inspector for 'Temp Button' properties - type anything into this field.
    3. Bam! The above warning appears. Remove the text from the field, and the warning disappears.

I've tried several solutions - different variable names, making the 'test' property an Optional, using an initializer, no luck so far.

Here is a screen capture of the problem:

enter image description here

Kennet answered 15/10, 2015 at 17:10 Comment(10)
Very interesting and I can't reproduce the issue. I did exactly what you said. No warnings. What version of Xcode are you using? Did you remember to build the app first? Did you try restarting the computer (helps to smack Xcode on the side of the head)?Championship
I have smacked Xcode on the side of the head(i.e. rebooted!), and I have tried it on both the current version of Xcode (7.0.1) and the beta (7.1 beta 3), no luck. Thanks for looking into it, interesting that it's not happening for you.Kennet
Have added a screen capture to help diagnose the problem, if other people aren't able to replicate.Kennet
Good screen capture. I see an important difference in our experiences: after you drag the button in and change its class, your Designables reads "Updating", but mine reads "Up to date". Surely that is the key difference. - Did you do the usual "blow the caches away" foo?Championship
Interesting observation, you're right, it hangs on 'Updating'. I've cleaned and rebuilt project, created new projects and the problem keeps occurring - any other suggestions on this 'blowing the caches away'?Kennet
As I explain here: https://mcmap.net/q/16662/-how-to-empty-caches-and-clean-all-targets-xcode-4-and-later No guarantees, of course, but always worth a go.Championship
Let us continue this discussion in chat.Kennet
Did you have any luck with this Craig? I've just wasted another day arsing around with xcode's idiosyncrasies.Snappy
Hi @amergin, in the end I had to move away from using IBInspectable's, which was a major pain. I thought I was the only person enduring as you said, this frustrating Xcode idiosyncrasy. I went through every suggestion of matt's to clear caches etc and nothing helped.Kennet
FWIW, i only experience this bug with UIButtonsDampen
P
9

I fixed this by ensuring the Interface Builder "build" did not have any errors. Here is what that means:

  1. In the left panel of Xcode, go to the Report Navigator (the last tab item at the top.)
  2. In the Reporst list, after your targets, you should see "Interface Builder". Expand that item.
  3. Ensure none of the items have errors (as designated by the red X).
  4. If you find an error here, select it and you will see more details of the problem. Fix that problem, clean, and rebuild.

Note that the issue if probably unrelated to the file you are working in. It appears that if the Interface Builder Build fails before it gets to building the file in question you will see these very cryptic warnings.

(My cause ended up being a Share Extension target referencing an AppIcon and SplashScreen asset catalog incorrectly.)

Parnas answered 12/1, 2016 at 22:16 Comment(2)
Gah! This means I have to insert @available to classes that's specific to iOS9+... and deal with older versions. So much work to just get Storyboard load properly. WTH.Hover
Close Xcode. Clear the DerivedData Folder "/Users/..../Library/Developer/Xcode/DerivedData" and build again.Cu
C
8

I face the same problem and I solved it. While you are open the storyboard: 1-click Editor on the upper menu. 2-click on Refresh All Views. 3-restart your application. 4-Build.

Copacetic answered 25/11, 2016 at 8:3 Comment(1)
this worked for me too on 8.2.1. it occured to me i had turned off auto-refreshing views when working offline recently to save battery, so maybe the issue came from that and having not refreshed the views recently.Rotenone
N
1

I had a similar issue but I didn't find the solution in the answers above. In my case there was an additional keyPath added by mistake in the Identity inspector:

enter image description here

Nace answered 21/4, 2020 at 7:29 Comment(1)
I've spent two days on "learning" how to build custom views with xib with an outdated Apple online docs. On this "tricky" way, I am collect a lot of try-outs (also with a property renaming), and seems some of them will add key-value attributes in different places like you mentioned. After wipe all wrong items - ontopic messages goes away. Thanks.Fixed
S
0

For your own good I hope you are not as stupid as I am, but I just spent 2 hours looking for a solution. In my case I had a property defined as a pointer to BOOL... oddly enough it gave me the same error.

I had to change from:

@property (nonatomic) IBInspectable BOOL *randomProp;

to:

@property (nonatomic) IBInspectable BOOL randomProp;
Samos answered 21/5, 2020 at 21:30 Comment(0)
R
0

If you encounter this issue, the solution that worked for me involves resetting the custom class assignment for the view. Here are the detailed steps to achieve this:

  1. In the storyboard, select the problematic view.
  2. Open the Inspector panel located on the right side of the Xcode interface.
  3. Navigate to the Identity Inspector, which is the fourth tab in the Inspector panel.
  4. In the Identity Inspector, you'll see a section titled 'Custom Class'. Here, clear out the existing class name in the 'Class' field.
  5. Once you've removed the class, reassign it by typing the class name back into the 'Class' field. As you begin typing, Xcode should offer autocompleted suggestions. Select your class from this list.

This effectively resets the link between the storyboard view and its corresponding class in the code, which should resolve the 'Designable Build Failed' error.

Rambling answered 27/7, 2023 at 9:56 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.