LoginSignup
11
10

More than 5 years have passed since last update.

IBDesignable, IBInspectableのエラーでハマった

Last updated at Posted at 2016-01-10

はじめに

Xcode6から追加されたInterfaceBuilder(以下IB)の機能であるIBDesignableとIBInspectableを使ってみて、ハマったエラーがあったので共有したいと思います。

IBDesignableとIBInspectableについては

などの記事が詳しく書かれているので参考にしてください。

エラー内容

Failed to set (myKey) user defined inspected property on (MyApp.MyClass): 
[<MyApp.MyClass 0x7fc1315478c0> setValue:forUndefinedKey:]: 
this class is not key value coding-compliant for the key myKey.

this class is not key value coding-compliant for the key myKey.のエラーについては、関連付けされたプロパティを編集・削除した際に、ストーリボードに古い関連が残っているエラーがほとんどだと思います。
この場合は、ストーリボード上から古い関連付けされたプロパティなどを削除してあげれば解決できます。

今回のは、Failed to set (myKey) user defined inspected property on(MyApp.MyClass):のエラーでIBInspectableのプロパティのエラーです。

警告の部分では以下のメッセージが

~/Main.storyboard: warning: IB Designables: 
Ignoring user defined runtime attribute for key path "myKey" on instance of "MyApp.Myclass". 
Hit an exception when attempting to set its value: [<MyApp.Myclass 0x7fb51b0104c0> setValue:forUndefinedKey:]: 
this class is not key value coding-compliant for the key myKey.

解決策

このエラーの解決法は場合によって異なると思いますが、自分の場合だとアクセスコントロールがprivateになっていることが原因でした。

    @IBInspectable private var myKey: Int {
        didSet {

        }
    }

普通に考えたらごくごく当たり前のことなのですが、IBInspectableのプロパティをprivateで設定しても、IBでは普通に値が設定できてしまうんですよね。ってことで簡単な問題に気付かず試行錯誤してました。

スクリーンショット 2016-01-10 13.05.20.png

普通にアクセスレベルを修正したら問題なく動作しました。
アクセスレベルはpublicinternalであれば問題ないです
(明示的にアクセスレベルを指定しなければ、自動的にinternalになる)

    @IBInspectable var myKey: Int {
        didSet {

        }
    }
11
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
10