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
NSUnknownKeyException', reason: ...: this class is not key value coding-compliant for the key X.'
Make sure you link lable_msg in (Contention inspector).
Xcode error: this class is not key value coding-compliant for the key *
Change the name of your controller to some name.
class UIView: UIViewController // This is wrong
class HomeViewController: UIViewController
: This is right
Also update the class of controller in storyboard
: Select controller in storyboard. Change file's owner
.
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
Resolve 'NSUnknownKeyException', reason:This class is not key value coding-compliant for the key X” exception
It looks like Visual Studio was putting the outlets in the wrong location. See below where i typed "Outlet was here". That is where the UI Label was generated. I manually removed it then went into the XIB Editor in visual studio and re-added the outlet name in the designer. At that point, it put it in it's right place where i labeled "When it should have been here".
I'm not sure what caused the behavior in the first place, but this work around definitely did the trick. Thanks to all who assisted me.
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="AlbumCell">
<connections>
***OUTLET WAS HERE***
</connections>
</placeholder>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" id="1" customClass="AlbumCell">
<rect key="frame" x="0.0" y="0.0" width="250" height="250"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" customColorSpace="calibratedWhite" colorSpace="calibratedWhite" white="0" alpha="1"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" text="Label" lineBreakMode="tailTruncation" minimumFontSize="10" id="4" translatesAutoresizingMaskIntoConstraints="NO" fixedFrame="YES" customClass="UILabel">
<rect key="frame" x="53" y="20" width="177" height="21"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" colorSpace="calibratedWhite" white="1" alpha="1"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<size key="freeformSize" width="600" height="600"/>
<connections>
***WHEN IT SHOULD HAVE BEEN HERE***
<outlet property="artistname" destination="4" id="name-outlet-4"/>
</connections>
</view>
</objects>
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.
Related Topics
How to Animate Constraint Changes
Undefined Symbols For Architecture Arm64
How to Run Cocoapods on Apple Silicon (M1)
Delete/Reset All Entries in Core Data
Uploading File With Parameters Using Alamofire
Convert HTML to Plain Text in Swift
Swift Compiler Error: "Expression Too Complex" on a String Concatenation
Get Location Updates For iOS App Even When Suspended
First and Last Day of the Current Month in Swift
Iphone 6 and 6 Plus Media Queries
Nsurlsession/Nsurlconnection Http Load Failed on iOS 9
Expand/Collapse Section in Uitableview in Ios
Add Uipickerview & a Button in Action Sheet - How
Background Location Services Not Working in iOS 7