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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:04:33+00:00 2026-05-25T22:04:33+00:00

Would like to create a method that instantiate objects. – (NSArray *) make3Of :

  • 0

Would like to create a method that instantiate objects. 

- (NSArray *) make3Of : (Class) type
{
    ...
    type * temp = [[type alloc] ...
    ...
}

But I get a warning from Xcode …

Actual warning:
“Class method +alloc not found (return type defaults to ‘id’)”

Is there a better/correct way to do this?

Actual code:

- (NSArray *) getBoxesOfType: (Class <ConcreteBox>) type StartingFrom: (uint64_t) offset
{
    NSMutableArray *valueArray = [[NSMutableArray alloc]initWithObjects: nil];

    for (uint64_t i = offset; i< boxStartFileOffset + self.size; i += [self read_U32_AtBoxOffset:i]) 
    {
        if ([[self read_String_OfLen:4 AtBoxOffset:offset + 4] isEqual:[type typecode]]) {

            [[type alloc]initWithFile:file withStartOffset:i]; //warning here; 

            //yes I plan to assign it to a variable 
            //(originally of "type" but that won't work as AliSoftware pointed out, will be using "id" instead.

            ...

        }
    }
}

Same as example, I’m trying to instantiate a couple of objects.

Code for protocol:

#import <Foundation/Foundation.h>

@protocol ConcreteBox

+ (NSString *) typecode;

- (id) initWithFile: (NSFileHandle *) aFile withStartOffset: (uint64_t) theOffset;

@end
  • 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-25T22:04:34+00:00Added an answer on May 25, 2026 at 10:04 pm

    You can’t use a variable (in your case type)… as a type for another variable!

    In your code, both type and temp are variables, that’s a syntax error.

    As you don’t know the type of the variable as compile time, use the dynamic type id instead. This type is specifically designed to handle cases when the type is not defined at compile time.

    So your code will look like this:

    -(NSArray*)make3Of:(Class)type {
      id obj1 = [[[type alloc] init] autorelease];
      id obj2 = [[[type alloc] init] autorelease];
      id obj3 = [[[type alloc] init] autorelease];
      return [NSArray arrayWithObjects:obj1, obj2, obj3, nil];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create a method that takes an event as an argument
I would like to create a safe sum extension method that would have the
I would like to create a method in a base generic class to return
I would like to create a String.replaceAll() method in JavaScript and think using a
I would like to create a class whose methods can be called from multiple
I would like create my own collection that has all the attributes of python
I would like create a web service in ASP.Net 2.0 that will supports JSON.
I'd like to create a structured type in Moose that can be used as
The Unity documentation states: if a class that developers instantiate using the Resolve method
Would like to create a strong password in C++. Any suggestions? I assume it

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.