Swift - strange code error

I have a problem and I could not find anyone else on the Internet with the same struggle, maybe this is too stupid.

I have the following class:

class UIXOColourPicker : UIView { @IBInspectable var moodValue: Int! ... } 

And in my storyboard, I have a look like this:

enter image description here

A custom runtime attribute was generated using the Xcode IBInspectable functionality (which is really cool by the way), but whenever I try to run the application, I get an error

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIXOColourPicker 0x7fa86877c420> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key moodValue.'

I am on Xcode 6 - Beta 6.

Any ideas?

Sorry if this is a stupid question, but I struggled with it like 2 hours and I don't have another Swift developer to look at it.

Greetings

+7
ios xcode swift xcode-storyboard
source share
2 answers

@IBInspectable uses cocoa objects, not native fast types. So, everything that is implicitly converted to a fast type should be cocoa. For Number or Bool you will need NSNumber . For something like Point , Size , Rect , etc. You need to use NSValue . However, for String you can directly use String ; you do not need to use NSString .

So, in your case, you need to use NSNumber instead of Int . Would I use NSNumber? instead of NSNumber! if no value is specified in your / xib storyboard.

 @IBInspectable var moodValue: NSNumber? 

Update

As @JakeLin and @Echelon noted, for Int like values, Xcode will only show an attribute in the Attributes Inspector if you declare it as Int? but then it will fail at runtime. Should you use NSNumber? , it will not be broken at run time, but the attribute will no longer be available in the Attributes Inspector; it will only appear in the attributes of the Runtime User Defined Runtime (this seems like an error in Xcode for me).

The error itself tells us how to get around this problem:

IBInspectable [66994: 58722469] Failed to set (moodValue) user-defined validation property (q25429792 ___ IBInspectable.ViewController): [setValue: forUndefinedKey:]: this class is not a key value compatible with the encoding for the key moodValue.

This means that the runtime cannot find an attribute compatible with key values ​​for the class for moodValue ( Int attributes do not meet the requirements associated with the key value), and that you can implement setValue:forUndefinedKey: to fix this.

In this case, the implementation may look something like this:

 @IBInspectable var moodValue: Int? override func setValue(value: AnyObject?, forUndefinedKey key: String) { if let value = value as? Int? where key == "moodValue" { self.moodValue = value } } 

So, if you really want the attribute to appear in the Attributes Inspector, and you do not mind adding an additional method, declare your property as Int? and implement setValue:forUndefinedKey: If you don't need an extra method, will you have to be content with using the NSNumber? attribute user interface NSNumber? and User Defined Runtime Attributes.

+13
source share

This applies to your type for moodValue . The KVO system seems unable to cope with the required type of enforcement; he is looking for moodValue with actual type NSNumber . Change

 @IBInspectable var moodValue: Int! 

to

 @IBInspectable var moodValue: NSNumber! 

Then choosing moodValue.integerValue should do what you want.

+6
source share

All Articles