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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:55:47+00:00 2026-05-21T22:55:47+00:00

I recently read about Dynamic Creation as one of the design pattern in Cocoa.

  • 0

I recently read about Dynamic Creation as one of the design pattern in Cocoa. However, I don’t really understand how it works. So I need clarification from you who have implemented in your design.

  1. What is it? Why and when would you use this design pattern?
  2. I have read that you use NSClassFromString() to access the class. I assume that I use this when I want to use class that doesn’t exist within the project I’m working on. Usually when I want to use certain class, I imported them in header. Does using this approach skip the #import process?

    Class JavaArrayList = NSClassFromString(@"java.util.ArrayList");
    

    I quote the code above as example. If do according to the code above, that means I can create a new JavaArrayList class and use the methods in it right?

    JavaArrayList *foo = [[JavaArrayList alloc] init];
    [foo useMethodBelongJava:doWhateverTask];
    
  3. What are the benefits of using this design pattern? Especially in iPhone Development.

  • 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-21T22:55:48+00:00Added an answer on May 21, 2026 at 10:55 pm

    Your example appears to be using that pattern to instantiate a Java class. In the old days (up to about MacOS 10.4 I think), Apple had some technology called the Cocoa-Java Bridge, which let you use Java classes within Objective-C code. You had to instantiate them in the manner specified, because they didn’t have Objective-C header files to import.

    However, as of Snow Leopard, the Java Bridge no longer exists, so the code in your question won’t work any more.

    The recommended solution for calling a Java class from Objective-C is now JNI. Take a look at this question if that is what you’re trying to do.

    1. What is it? Why and when would you use this design pattern?

    Coming back to NSClassFromString, it has other uses besides instantiating Java classes (which, as I mentioned, it doesn’t do any more!). For an example, recently I wrote a library for parsing the response from a web service. In order to make it work with different web services, I had it read in a configuration file that described the data format it was expecting. For each field in the web service, my configuration file specified which Cocoa class to instantiate. Thus, in my code, I had a Cocoa class name as a string. To instantiate the object I wanted, I used NSClassFromString to turn it into a Class object.

    1. Usually when I want to use certain class, I imported them in header. Does using this approach skip the #import process?

    It can do. NSClassFromString will instantiate any class that is present at run time, so you don’t need the header to be able to use it. If you don’t have the header, you’ll get a bunch of warnings of “may not respond to selector” whenever you try and use your newly instantiated class, as the compiler doesn’t have enough information to be helpful. However, in many circumstances where NSClassFromString is useful, the header files aren’t available.

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

Sidebar

Related Questions

I recently read a question on here about static and dynamic linking, which reminded
I've read a lot recently about how writing multi-threaded apps is a huge pain
I recently read a nice post on using StringIO in Ruby. What the author
I recently read somewhere that writing a regexp to match an email address, taking
I recently read a discussion regarding whether managed languages are slower (or faster) than
I recently read Ayende's blog post on automatic registration working with XML Configuration. I
I recently read this thread on MSDN . So I was thinking of using
I recently read Object Oriented Exception Handling in Perl Perl.com article. Is there any
I've recently read the Yahoo manifesto Best Practices for Speeding Up Your Web Site
Ever wonder what wikipedia's database schema looks like? I recently read this thread from

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.