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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:16:46+00:00 2026-06-01T00:16:46+00:00

With generics on languages like C# or Java, you can have a factory that

  • 0

With generics on languages like C# or Java, you can have a factory that returns a result depending on the given type? For example you can tell the factory method to return:

Book
List<Book>
Door
List<Door>

Is it possible to achieve the same thing with objective-c?
Can I somehow tell generateObjects method to return me an array of books?

[self getDataFromWeb:@"SOME_URL" andReturnResultWithType:[Book class]];
// What about an array of Books?

- (id)getDataFromWeb:(NSString*)url andReturnResultWithType:(Class)class
{
    // Convert JSON and return result
    // Mapping conversion is a class method under each contract (Book, Door, etc)
} 

Let’s say this is one of my data contracts

@interface Book : JSONContract

@end

@implementation Book

+ (NSDictionary *)dataMapping
{
   // returns an NSDictionary with key values
   // key values define how JSON is converted to Objects
}

@end

EDIT:

Modified the examples to be more clear

  • 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-01T00:16:47+00:00Added an answer on June 1, 2026 at 12:16 am

    No, it is no possible to say that your array will contain String
    But, Yes, it is possible to create String based on a Class definition or even a class name.

    Objective-C as “reflection” capabilities like Java, it is called “introspection”

    For example, you can create an object based on its class name using this code

    NSString* myString = (NSString*)[[NSClassFromString(@"NSString") alloc] init];
    

    NSClassFromString is documented here :
    https://developer.apple.com/library/mac/#documentation/cocoa/reference/foundation/miscellaneous/foundation_functions/reference/reference.html

    If you want the compiler to check types for you, you can also directly use the Class object, as this

    Class stringClass = [NSString class];
    NSString* myString = [[stringClass alloc] init];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone tell me why ActionScript 3, a statically typed language, doesn't have generics?
Take the following generics example import java.util.List; import java.util.ArrayList; public class GenericsTest { private
Is there anything that java cannot do? But other's can? I am talking about
I am aware that by default Java does not have the so-called eval (what
I have several objects that look like this: class PhoneNumber { String getNumber(); String
Many programming languages share generic and even fairly universal features. For example, if you
Java has generics and C++ provides a very strong programming model with template s.
I have not used generics much and so cannot figure out if it is
Is there a Generics Friendly way of using Collection.EMPTY_LIST in my Java Program. I
this question feels like it would have been asked already, but I've not found

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.