prabodhprakash prabodhprakash - 5 days ago 6
Swift Question

NSClassForString for Swift classes with target name having space in it

As per document here: Using Swift With Cocoa and Objective-C
we need to have fully qualified name to get a class from

NSClassFromString()


For e.g. if the target name is ABC and class name is XYZ we get the class by calling `NSClassFromString(ABC.XYZ)

However, I am not able to get the results when there is a space in the name of the target, i.e. when target name is something like
dummy target
. How do we get class in such scenario?

It is valid for a target name to have spaces, like shown in figure below
enter image description here

Answer

The fully qualified class name is

 <product module name>.<class name>

Xcode derives a module name from the target name when a new target is created. If the target name is a valid module name then both will be equal by default. Otherwise Xcode replaces invalid characters by an underscore. The product module name can also be configured in the build settings.

Example:

  • Target name: "ABC DEF",
  • Product module name: "ABC_DEF",
  • Class name: "MyClass",
  • Fully qualified class name: "ABC_DEF.MyClass"
Comments