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

  • Home
  • SEARCH
  • 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 852651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:37:58+00:00 2026-05-15T07:37:58+00:00

I’m trying to create a universal iPhone app, but it uses a class defined

  • 0

I’m trying to create a universal iPhone app, but it uses a class defined only in a newer version of the SDK. The framework exists on older systems, but a class defined in the framework doesn’t.

I know I want to use some kind of weak linking, but any documentation I can find talks about runtime checks for function existence – how do I check that a class exists?

  • 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-15T07:37:59+00:00Added an answer on May 15, 2026 at 7:37 am

    TLDR

    Current:

    • Swift: if #available(iOS 9, *)
    • Obj-C, iOS: if (@available(iOS 11.0, *))
    • Obj-C, OS X: if (NSClassFromString(@"UIAlertController"))

    Legacy:

    • Swift (versions prior to 2.0): if objc_getClass("UIAlertController")
    • Obj-C, iOS (versions prior to 4.2): if (NSClassFromString(@"UIAlertController"))
    • Obj-C, iOS (versions prior to 11.0): if ([UIAlertController class])

    Swift 2+

    Although historically it’s been recommended to check for capabilities (or class existence) rather than specific OS versions, this doesn’t work well in Swift 2.0 because of the introduction of availability checking.

    Use this way instead:

    if #available(iOS 9, *) {
        // You can use UIStackView here with no errors
        let stackView = UIStackView(...)
    } else {
        // Attempting to use UIStackView here will cause a compiler error
        let tableView = UITableView(...)
    }
    

    Note: If you instead attempt to use objc_getClass(), you will get the following error:

    ⛔️ ‘UIAlertController’ is only available on iOS 8.0 or newer.


    Previous versions of Swift

    if objc_getClass("UIAlertController") != nil {
        let alert = UIAlertController(...)
    } else {
        let alert = UIAlertView(...)
    }
    

    Note that objc_getClass() is more reliable than NSClassFromString() or objc_lookUpClass().


    Objective-C, iOS 4.2+

    if ([SomeClass class]) {
        // class exists
        SomeClass *instance = [[SomeClass alloc] init];
    } else {
        // class doesn't exist
    }
    

    See code007’s answer for more details.


    OS X or previous versions of iOS

    Class klass = NSClassFromString(@"SomeClass");
    if (klass) {
        // class exists
        id instance = [[klass alloc] init];
    } else {
        // class doesn't exist
    }
    

    Use NSClassFromString(). If it returns nil, the class doesn’t exist, otherwise it will return the class object which can be used.

    This is the recommended way according to Apple in this document:

    […] Your code would test for the
    existence of [a] class using
    NSClassFromString() which will return
    a valid class object if [the] class
    exists or nil if it doesnʼt. If the
    class does exist, your code can use it
    […]

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

Sidebar

Ask A Question

Stats

  • Questions 424k
  • Answers 424k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer http://www.fileinfo.com/extension/do According to WikiPedia, .do is: a pseudo file extension… May 15, 2026 at 11:55 am
  • Editorial Team
    Editorial Team added an answer You can make each of your weights a pair: (#… May 15, 2026 at 11:55 am
  • Editorial Team
    Editorial Team added an answer To perform the last two steps, I recommend you use… May 15, 2026 at 11:55 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.