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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:05:45+00:00 2026-05-12T07:05:45+00:00

I have an designated initializer with optional arguments (similar to the following code), and

  • 0

I have an designated initializer with optional arguments (similar to the following code), and I want to create an autorelease method by calling it. Is there any way to do this?

@interface MyObject : NSObject

- (id)initWithArgs:(id)firstArg, ...;
+ (id)objectWithArgs:(id)firstArg, ...;

@end

@implementation MyObject

- (id)initWithArgs:(id)firstArg, ...
{
    if (!firstArg || ![super init]) {
        return nil
    }

    va_list argList;
    va_start(argList, firstArg);

    id currentObject = firstArg;
    do {
        NSLog(@"%@", currentObject);
    } while ((currentObject = va_arg(argList, id)) != nil);

    va_end(argList);
    return self;
}

+ (id)objectWithArgs:(id)firstArg, ...
{
    // return [[[MyObject alloc] initWithArgs:firstArg, ...] autorelease];
}

@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-12T07:05:45+00:00Added an answer on May 12, 2026 at 7:05 am

    You can’t do it. See the comp.lang.c FAQ. The closest thing you can do is to create two versions of your functions, one which takes varargs (...), and one which takes a va_list. The varargs version can then pass off the work to the va_list version to avoid duplicate code:

    @interface MyObject : NSObject
    
    - (id)initWithArgs:(id)firstArg, ...;
    - (id)init:(id)firstArg withVaList:(va_list)args;
    + (id)objectWithArgs:(id)firstArg, ...;
    + (id)object:(id)firstArg withVaList:(va_list)args;
    
    @end
    
    @implementation MyObject
    
    - (id)initWithArgs:(id)firstArg, ...
    {
        va_list args;
        va_start(args, firstArg);
        id result = [self init:firstArg withVaList:args];
        va_end(args);
        return result;
    }
    
    - (id)init:(id)firstArg withVaList:(va_list)args
    {
        // do actual init here:
        while((id arg = va_arg(args, id)))
        {
            // ...
        }
    }
    
    + (id)objectWithArgs:(id)firstArg, ...
    {
        va_list args;
        va_start(args, firstArg);
        id result = [MyObject object:firstArg withVaList:args];
        va_end(args);
        return result;
    }
    
    + (id)object:(id)firstArg withVaList:(va_list)args
    {
        return [[[MyObject alloc] init:firstArg withVaList:args] autorelease];
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following designated initializer: -(id) initWithName:(NSString*)name; and the following default initializer: -(id)
I have searched the UIImage class for it's designated initializer, but there wasn't one.
I have Designed the Javascript function My.js it contains following code My.js function display(){
I want to create an Application with tabs, and I have found this guide
I have a class whose new method has been made private because I want
I have the following code but can't compile it because I have a Type
I have the following code that gets initialized as a static variable in a
I have clustered applications that requires one of the nodes to be designated as
I have designed a new web site. I have hosted it online. I want
I have a code that was working until iOS 4.3.5, now in iOS 5

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.