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 6538329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:43:24+00:00 2026-05-25T10:43:24+00:00

I have some Code that I’d like to share between an iOS and an

  • 0

I have some Code that I’d like to share between an iOS and an OSX project. Unfortunately, some classes, while functionally relatively similar (and having greatly overlapping interface definitions), are parts of different hierarchies and frameworks, and have different names. UIKit/AppKit classes are a prime example.

One solution would be to use preprocessor directives, but that creates a lot of duplicate code:

#if TARGET_OS_IPHONE
  - (UIImage *)getImage; //have to create an implementation for each
#else
  - (NSImage *)getImage; //have to create an implementation for each
#endif;

I had thought about using reflection. In Objective-C, it’s possible to dynamically get a Class object with the use of NSClassFromString(@"name"). This is very nice, but if used often, creates ugly code. It also can’t be used in a header file, because it’s not constant, so this does not work:

//defined once per project
#if TARGET_OS_IPHONE
  Class NUImage = NSClassFromString(@"UIImage");
#else
  Class NUImage = NSClassFromString(@"NSImage");
#endif;

//usage
- (NUImage *)getImage; //does not compile

I want to be able to define (ideally at compile time) a Class Synonym, so that it can be used transparently in place of the specific implementation class. Is this possible? In essence, I want to avoid having to write a wrapper class (which would either just forward messages to the concrete implementation, thus be easy to write but take away code completion and checking, or be a lot of boilerplate code just to duplicate the interface of the class(es) it’s wrapping).

Is it possible to store the Class as a constant?

  • 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-05-25T10:43:24+00:00Added an answer on May 25, 2026 at 10:43 am

    You should try something like:

    #if TARGET_OS_IPHONE
    typedef UIImage NUIImage; // NUIImage is an alias for UIImage
    #else
    typedef NSImage NUIImage; // NUIImage is an alias for NSImage
    #endif
    

    And then use NUIImage everywhere you would have used either NSImage or UIImage. Typedef is inherited from C, so it’s a compile cost only with no impact upon the runtime (so, e.g. NSClassFromString(@"NUIImage") would return nil all other thing being equal). It causes the compiler to consider the one thing to be an alias of the other. If you were adopting strict C conventions you’d probably call it NUIImage_t.


    The following is an edited version of my original answer, based on an original, mistaken reading that the question hinged on using a specific class that may or may not exist, with that fact not known until runtime. It’s accurate for that sort of situation, but not especially relevant:

    You don’t need to find a solution that works in method definitions or in any of the other definitions normally used in a header file; you can just use the class name there. In Objective-C you always refer to objects by pointer and all pointers look the same to the hardware. The use of typed pointers in source code is therefore a bit of a fiction for the benefit of the programmer (including indirectly via relevant compiler warnings) with no effect on the generated binary.

    As long as a definition like the following compiles, it doesn’t matter whether neither SomeClass nor SomeOtherClass are available at runtime:

    - (SomeClass *)someMethod:(SomeOtherClass *)argument;
    

    You won’t get an exception just for having declared the method.

    The only place you really need to use NSClassFromString is wherever you make an initial branch on whether or not a type of object is defined. For example, you can write the following class:

    @interface SomeClassThatUsesSomeOtherClass
    
    - (id)init;
    
    @property (nonatomic, readonly) SomeOtherClass *otherClassInstance;
    
    - (void)doSomeTask;
    - (SomeOtherClassResult *)doSomeOtherTask;
    
    @end
    
    /* elsewhere */
    
    @implementation SomeClassThatUsesSomeOtherClass
    
    @synthesize otherClassInstance;
    
    - (id)init
    {
        // blah blah blah, standard init stuff here
    
        // switch is made implicitly here, because if we get nil back
        // then we'll end up with nil in 'otherClassInstance' — we're
        // considering the class not existing to be equivalent to any
        // other issue that might cause an instance not to be created
        otherClassInstance = [[NSStringFromClass(@"SomeClass") alloc] init];
        if(!otherClassInstance)
        {
            [self release];
            return nil;
        }
    
        // blah blah blah, rest of standard init
    }
    
    // you'll need a suitable dealloc, too
    
    - (void)doSomeTask
    {
        [self.otherClassInstance doAThing];
        [self.otherClassInstance doAnotherThing];
    }
    
    - (SomeOtherClassResult *)doSomeOtherTask
    {
        return [self.otherClassInstance doAThirdThing];
    }
    
    @end
    

    And your code is completely safe, automatically working or not working depending on whether the class is available.

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

Sidebar

Related Questions

I have some code that looks like my ($ids,$nIds); while (<myFile>){ chomp; $ids.= $_
I have some code that raises PropertyChanged events and I would like to be
I have some code that looks like: template<unsigned int A, unsigned int B> int
I have some code that is basically 5 print statements that i would like
I have some code that looks like this: <h3>Thursday Sep 10</h3> <ul>great stuff in
I have some code that looks something like this: <? $addr = array(city =>
I have some code that looks something like this: d = {'foo': True, 'bar':
I have some code that looks like this (I left out the parts that
I have some code that I would like to run only once in my
I have some code that gives a user id to a utility that then

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.