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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:28:34+00:00 2026-05-16T10:28:34+00:00

I’m making a class that initializes instances of certain classes. This class will be

  • 0

I’m making a class that initializes instances of certain classes. This class will be used to initialize a few different types of classes all subclassed from a common super class. Currently I am using an instance variable:

Class templateClass;

to store the class type. I get compiler warnings saying methods aren’t supported by templateClass. Is there a way to suppress these warnings or specify that the template class must be a subclass of a certain class?
Also, is this the ideal way to go about this in objective-c? Is there a different way to specify the class to use that I should use?
Thanks for the help!

Edit:
The class I am writing is designed to create sprites in randomly chosen locations with a few variables to limit them. This class expects the class it rezzes to have an initWithX:(int)pos yVariance:(int)variance method. I expect this can be defined using the protocals you mentioned. I will be making multiple instances of the rezzing class for each type of sprite I must create. I want to be able to send the rezzing class initWithTemplateClass:(Class)templateClass to define the type of sprite to create. I’m not sure if this is the correct way to go about this though because the compiler posts that the method initWithX:(int)pos yVariance:(int)variance is not available for the templateClass. I expect there is a problem in the way I am going about this.
The code that is showing the warning is:

[self addChild:[[templateClass alloc] initWithX:positionOfChild  
  yVariance:(random()%(rowVarianceSize+1))]];
  • 1 1 Answer
  • 1 View
  • 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-16T10:28:35+00:00Added an answer on May 16, 2026 at 10:28 am

    Edited to add a 2nd alternative

    I don’t get the that warning (nor did I expect to) when I compile a simple test case that attempts to do what you’re doing.

    That said, what you’re doing seems perfectly reasonable. Though you do have a leak in the ‘addChild’ example. You alloc/init your new object which leaves it with a retain count of 1, then (assuming ‘self’ is a UIView) your call to addChild will also retain the object, but you have no reference of your own to release.

    id obj = [[templateClass alloc] initWithX:positionOfChild yVariance:(random()%(rowVarianceSize+1))]];
    [self addChild:obj];
    [obj release], obj = nil;
    

    Also, Vladimir’s suggestion to define a protocol is a good one, and if you do so, you can even insert a runtime check that the templateClass you were given conforms to that protocol.

    if ([templateClass conformsToProtocol:@protocol(Foo)]) {
        id<Foo> obj = [[templateClass alloc] initWithX:positionOfChild yVariance:(random()%(rowVarianceSize+1))]];
        [self addChild:obj];
        [obj release], obj = nil;
    } else {
        // Do whatever you want to do if templateClass isn't correct.
        // Obviously you could also have the protocol conformance check elsewhere.
    }
    

    However, as an alternate to all of that, instead of using your Class instance variable, you could define an interface for a factory object that creates the particular type of sprite you want. Then pass in an appropriate factory class instance.

    For example:

    @interface Sprite : UIView {
    }
    - (id)initWithX:(int)pos yVariance:(int)variance;
    @end
    
    @interface SpriteFactory {
    }
    - (Sprite*)createSprite;
    @end
    

    Then create pairs of subclasses: a Sprite subclass, and a SpriteFactory subclass which creates that kind of sprite.

    Then instead of using your ‘Class templateClass’ you would have a ‘SpriteFactory* factory’ instead.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I need a function that will clean a strings' special characters. I do NOT
I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small

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.