Why Does My Swift Bundle Get The Wrong Principal Class

Why does my Swift bundle get the wrong principal class?

Somehow, using its qualified name instead ("PlugInNamespace.PrincipalClass") solved the problem, but this has me very skeptical because I have another plugin that does not requires this.

How to set Principal Class for a loadable bundle?

The solution to the issue is rather simple (if we exclude my own issues related to Xcode bugs not actually updating the changes made to the initial project).

As @AnoopVaidya also pointed out, just open up your info.plist, and even if the NSPrincipalClass key is not there, set its value to the name of your actual "Principal Class".

Once you Clean and Re-Build, your bundle's principal class should be properly recognised.

Documentation :

NSBundle finds the principal class in one of two ways. First, it looks
for the NSPrincipalClass key in the bundle’s information property
list. If the key is present, it uses the class named by the key’s
value as the bundle’s principal class. If the key is not present or
the key specifies a class that does not exist, NSBundle uses the first
class loaded as the principal class. If the bundle is built with
Xcode, the order of classes as viewed in the project determines the
order in which they are loaded.

Source :

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/LoadingCode/Concepts/CocoaBundles.html#//apple_ref/doc/uid/20001269-BAJCIAHA

Unable to run app in Simulator : An error was encountered while running (Domain = LaunchServicesError, Code = 0)

This happens if your extension's bundle ID isn't prefixed with your app's bundle ID. For example if you app is com.mycompany.appname, your extension should be something like com.mycompany.appname.today.

Something else to note, if you're using Swift and are setting a principal class with NSExtensionPrincipalClass, you'll want to make sure your extension target sets "Defines Module" to "Yes" and make the value of NSExtensionPrincipalClass equal to "YourModuleName.YourClassName".

How to fix that error “Fatal Error in ContentView.swift”?

The reason for this failing is either the url for "start.txt" is nil or try? String(contentsOf: startWordsURL) fails. In order to help you debug and become an understanding why your current code design is bad consider this design:

func startGame() {
// 1.find the URL for start.txt in our app bundle
guard let startWordsURL = Bundle.main.url(forResource: "start",withExtension: "txt") else{
//assign the default value
rootWord = "yamori"
// if the url is not found this will print in the console
print("File could not be found")
return
}

do{
// 2.load start.txt into a string
let startWords = try String(contentsOf: startWordsURL)
// 3.split the string up into an array of strings, splitting on line breaks
let allWords = startWords.components(separatedBy: "\n")
// 4.pick one random word, or use "yamori" as a sensible default
rootWord = allWords.randomElement() ?? "yamori"
} catch{
//if the file couldn´t be read this will print the error in the console
print(error)
}
}

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



Related Topics



Leave a reply



Submit