Thursday, March 19, 2015

NSClassFromString and Spaces in Product Name

If you are using NSClassFromString to get classes by "Product Name"."Class Name" in iOS, watch out for spaces in your product name. All spaces need to be converted to underscores before passing the string to NSClassFromString.

NSString *className = @"My App Name.MyClassName";
Class clazz = NSClassFromString(className);


The clazz will be nil because of the spaces in "My App Name". The className parameter should be "My_App_Name.MyClassName".

I ran into this problem using Typhoon. Typhoon is Dependency Injection framework for iOS. It has a PList integration functionality where the TyphoonAssembly is created based on the string in the PList. This started failing because I created a project with spaces in the name.

I made the small fix and sent a pull request, https://github.com/appsquickly/Typhoon/pull/328 and the Typhoon people merged the PR really fast.

No comments:

Post a Comment