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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:29:33+00:00 2026-05-17T17:29:33+00:00

I always wonder, should we use NSClassFromString before using any built in class in

  • 0

I always wonder, should we use NSClassFromString before using any built in class in our code to make is generic. Or is there any standard practice to use this. Same thing for property. Should we use valueForKey and setValue calls separately to access any property. Is it a standard practice?

For example look at the below code

// Screen scaling
  if ([self respondsToSelector:@selector(setContentsScale:)])
  {
   Class screenClass = NSClassFromString(@"UIScreen");
   if ( screenClass != Nil)
   {
    id scale = [[screenClass mainScreen] valueForKey:@"scale"];
    [(id)self setValue:scale forKey:@"contentsScale"];
   }
  }

Here everything is checked before use. Is it required to do this and what about performance?

  • 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-17T17:29:33+00:00Added an answer on May 17, 2026 at 5:29 pm

    No, this is only for classes which you don’t know whether they do exist in your environment or which are dynamically loaded at runtime (something iOS doesn’t allow but is available on Mac OS X, so you don’t need to care about that).

    You need to do this e.g. if you want to support 3.0 devices, but use 4.x classes if they are available, like GameKit classes. Since UIScreen is available since 2.0 you don’t need to use NSClassFromString here. But you would need to check for the presence of scale since it was added in 4.0, and valueForKey:@"scale" will raise a NSUndefinedKeyException on pre-4.0 devices.

    Instead of key-value coding, you could also do this:

    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        CGFloat scale = [[UIScreen mainScreen] scale];
        // or scale = [UIScreen mainScreen].scale;
        // Now do something with the scale.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is a question that when programming I always wonder: What to use when
Whenever I design a database, I always wonder if there is a best way
I distribute software online, and always wonder if there is a proper way to
I always wonder why compilers can't figure out simple things that are obvious to
Even though I always strive for complete validation these days, I often wonder if
I always believe they did, but seeing some answers here make me doubt... Can
I always tend to forget these built-in Symfony functions for making links.
So I am doing a lot of php tutorials and I always wonder when
Always was interested why are Array.Sort() and Array.IndexOf() methods made static and similar ArrayList.Sort()
Always when I run java application it will display in Windows Task Manager is

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.