Xcode - How to fix 'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X error?
Your view controller may have the wrong class in your xib.
I downloaded your project.
The error you are getting is
'NSUnknownKeyException', reason: '[<UIViewController 0x3927310> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key string.'
It is caused by the Second
view controller in MainWindow.xib
having a class of UIViewController
instead of SecondView
. Changing to the correct class resolves the problem.
By the way, it is bad practice to have names like "string" in Objective-C. It invites a runtime naming collision. Avoid them even in once off practice apps. Naming collisions can be very hard to track down and you don't want to waste the time.
Another possible reason for this error: when copying & pasting elements from one controller into another, Xcode somehow keeps that link to the original controller, even after editing & relinking this element into the new controller.
Another possible reason for this error:
Bad Outlet.
You have either removed or renamed an outlet name in your .h
file.
Remove it in .xib
or .storyboard
file's Connection Inspector.
One more possible reason
(In my case) Extension of UIView with bindable properties and setting values for those bindable properties (i.e. shadow, corner radius etc.) then remove those properties from UIView extension (for some reason) but the following <userDefinedRuntimeAttributes>
remained in xml (of foo.storyboard
):
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="color" keyPath="shadowColor">
<color key="value" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="shadowOpacity">
<real key="value" value="50"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="point" keyPath="shadowOffset">
<point key="value" x="5" y="5"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="shadowRadius">
<real key="value" value="16"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="borderWidthValue">
<real key="value" value="0.0"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
Solution: Right click on foo.storyboard
> Open as Source Code > search by keyPath (i.e. shadowRadius) > Delete the </userDefinedRuntimeAttributes>
that causing the problem
Swift this class is not key value coding-compliant for the key Error
There are several steps to define a XIB and use it programmatically.
First you need to create XIB file and the Swift file with the same name.
Design as you need, then hook up the file's owner as the class name you have defined in Step 1.
You need to define a common initializer method and override both initializers (with frame and coder).
Hook up the whole view in XIB as IBOutlet in Swift class.
Implement common initializer as follows:
private func commonInitializer() {
Bundle.main.loadNibNamed("testView", owner:self, options: nil)
addSubview(contentView)
contentView.frame = self.bounds
contentView.autoresizingMask = [.flexibleHeight,.flexibleWidth]
}
- Finally, you can drag a UIView to anywhere and set its class as this custom class.
For more details, you can check out this tutorial here
This class is not key value coding-compliant for the key error in Swift
See: Thread 1: signal SIGABRT Xcode 6.1
You have to go into Interface Builder and look for the one (or more) outlets that have a warning triangle (follow the link for a screenshot). Once you delete those bad connections, you're either (1) ready to go because you have already connected your new objects or (2) you need to make the new connections so that you have all the elements loaded properly and you have no warning triangles.
NSUnknownKeyException this class is not key value coding-compliant for the key
"this class is not key value coding-compliant for the key pause" usually means you have a referencing outlet problem. Look in the Connections Inspector for your different buttons. You may likely either have:
2 referencing outlets for one button and the program does not know which ne to use, etc.
I ran the code hooking up 1 label to an IBOutlet and three buttons (play, pause, reset), one to each IBAction, and it ran perfectly.
How to fix Error: this class is not key value coding-compliant for the key tableView.'
You have your storyboard set up to expect an outlet called tableView
but the actual outlet name is myTableView
.
If you delete the connection in the storyboard and reconnect to the right variable name, it should fix the problem.
How can I fix this: this class is not key value coding-compliant for the key imageview?
The problem is revealed in the error message:
[UIViewController 0x7fd98fc65ee0> setValue:forUndefinedKey:]:
So you need to set the class of this view controller in the storyboard to the class of the real view controller that has the background
property.
In all probability you had this correctly set already, but it was working in Objective-C only. If you go into the Identity inspector and delete the class, and hit return, and then enter the class again, and hit return, you'll see that now the Identity inspector sees that we are supposed to get this class from the Swift module, and things will start working in Swift.
Basically the root cause is probably Swift’s name mangling. An Objective-C class called MyClass is really called MyClass, but a Swift class called MyClass isn’t. Removing the name and reentering it shows Objective-C the class’s new name.
This class is not key value coding-compliant for the key cancel
This means that you have something on your storyboard connected to the IBOutlet called cancel
but you don't have this IBOutlet in your class. So compiler can't find Key cancel
(it means property) in your class. You should find that button(i think it's a UIButton because of the name) in storyboard, click right mouse button on it and click "x" to delete that connection. Or you might want to delete this button at all. Or you might want to add this IBOutlet to your class.
this class is not key value coding-compliant for the key authView
something went wrong when in xcode 4 with some reference , cause i declared IBOutlet that reference authView then run and it worked!, then i removed the IBOutlet declaration and worked well
Related Topics
Unsafemutablepointer in Swift as Replacement for Properly Sized C Array in Obj-C
Swift - Get Nsbundle for Test Target
Get Location from Center of Screen Swift Mapkit
How to Repeat Animation (Using Uiviewpropertyanimator) Certain Number of Times
Translucent Status Bar with No Navigation Bar
Access Properties via Subscripting in Swift
Enum with Identical Cases Names with Associated Values of Different Types
Reason for Assigning Optional to New Variable in Conditional Statement in Swift
Typecasting or Initialization, Which Is Better in Swift
Swift - Class Method Which Must Be Overridden by Subclass
Understanding the Userdefaults Register Method
With Firebase, Swift Removeobserver(Withhandle Does Not Remove the Observer
Subclass Nsapplication in Swift
Aws S3 Transfer Manager ${Cognito-Identity.Amazonaws.Com:Sub} Policy Variable Access Denied
Pass Optional Block or Closure to a Function in Swift