Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8964337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:33:54+00:00 2026-06-15T16:33:54+00:00

I have a testing class within my project that is there during the development

  • 0

I have a testing class within my project that is there during the development phase but will be compiled out for the app store build.

I’ve read in several books that NSClassFromString can be used to determine if a class exists with example code such as:

if (NSClassFromString(@"SomeClass") != nil)
{
[SomeClass someMethod];
}
else
{
code not using SomeClass
}

However after putting this into practice in my project, I find that I’m getting a linking error for the target where I have removed the source file containing SomeClass.

Therefore my conclusion is this mechanism for determining the presence/absence of a class is only useful if that class is within a library or source file where you are using other classes ( a detail which is missing from the books that mention this technique).
Or am I missing something?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:33:56+00:00Added an answer on June 15, 2026 at 4:33 pm

    NSClassFromString searches the runtime for the named class. Ipso facto it will determine if the class exists.

    For the compiler successfully to get past static references like [SomeClass someMethod] it needs something to link against.

    You’re right that the mechanism therefore works when you link to something like Foundation and Apple adds an extra class there, like checking whether NSJSONSerialization exists.

    It also works if you weak link against a framework that may or may not be present. So e.g. you could weak link against Twitter.framework and use [TWRequest alloc] .... You’d still be able to build and deploy on iOS 4 where there wasn’t a Twitter framework at all.

    Apple have actually slightly modified the way that libraries are stored and are transitioning to allowing if([TWRequest class]) or whatever to be used in place of the explicit NSClassFromString where you weak link.

    However in your case it sounds like you want to either link to a static library or not link to it? So the class may or may not be available at runtime but also may or may not be visible to the linker?

    In that case you just need to avoid literal references to the metaclass. So you could do something like:

    Class someClassMetaClass = NSClassFromString(@"SomeClass");
    
    if(someClassMetaClass)
    {
        SomeClass *instance = [[someClassMetaClass alloc] init];
        /* ... */
    }
    

    As long as the header file is visible the compiler will be happy allowing you to specify the SomeClass type for pointers (because that doesn’t go into the binary anyway) and will be able to suggest the usual autocompletions and warnings against the published interface. Just make sure you always address the metaclass through a pointer to the Class that you got from the runtime rather than directly. That way there’s nothing that the linker needs to follow up on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm testing a C++ class with a number of functions that all have basically
I have a dummy class where I am testing arrays. I've noticed that when
In my testing project, I have a static class called FixtureSetup which I use
This is a MVC 3 project. Just for testing, I have public class MyRoleProvider
I have a Testing project that contains a csv file which a webtest uses
I have been assigned the task of unit testing a class that I never
I have a class I'm unit testing that requires fairly extensive database setup before
Please help with ActiveRecord testing. Trying my first Rails 3.1.0 project. There I have
I am writing some unit test and I have a unit testing base class
I have two classes that I am testing (let's call them ClassA and ClassB).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.