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

  • Home
  • SEARCH
  • 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 9250279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:24:35+00:00 2026-06-18T10:24:35+00:00

In Objective C, a custom init method must call the superclass’s designated initializer. Consider

  • 0

In Objective C, a custom init method must call the superclass’s designated initializer. Consider the following custom init method for a sublcass of NSView:

- (void)initWithFrame:(CGRect)aFrame andName:(NSString *)aName {
    if(self = [super initWithFrame:aFrame]){
        //set up object
    }
}

However, MacRuby only offers the super keyword, which simply tries calling the same method in the superclass. Since this is a custom initializer, however, the superclass has no such method.

What is the standard way of solving this? I did see this question: MacRuby custom initializers but I can’t say I understand the answer very well, nor does it seem to be some broadly accepted solution. Since MacRuby is now a few years older than when that post was written, I’m hoping a clearer, standard solution now exists.

  • 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-18T10:24:36+00:00Added an answer on June 18, 2026 at 10:24 am

    In rubymotion (and I think MacRuby shares this behavior) there are TWO types of classes: pure Ruby, which use initialize, and classes derived from an objective-c class. It is best not to mix the two. If you are subclassing a objective-c class, you should use an init method. your initialize method will never get called!

    This is because the NSObject##new method calls init, not initialize (actually even this is an oversimplification, but the abstraction works).

    your example above is confusing because you are calling init instead of initWithFrame. You MUST call the designated initializer of the parent class – in this case, using super.

    If the initializer you call (in this case, init, calls your initializer, you’re using the wrong one – there’s no escaping the recursion cycle in that case.

    So you can’t exactly CHOOSE which method to call on the parent. You can, however, use your own init method:

    def initWithTitle(title)
      initWithFrame(CGRectZero)
      @title = title
      return self
    end
    

    I would write this method like this:

    def initWithTitle(title)
      initWithFrame(CGRectZero).tap do
        @title = title
      end  # that way I never forget to return self
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To help simplify debugging of some custom Objective-C objects in the Xcode debugger window,
Question We're developing a custom EventEmitter inspired message system in Objective-C. For listeners to
How can I add additional, custom information to my objective-c methods and properties? I
Objective-C uses a sophisticated message-passing system when one object calls a method on another
i am writing my first custom class in objective-c and when i am trying
The objective is to create a custom route so that /undead will go to
I've created a custom class with various methods in objective-c and I've also added
Possible Duplicate: In Objective-C why should I check if self = [super init] is
I'm new to Objective-C and trying to create custom cells. In the storyboard (under
I'm creating a custom subclass of UIView. I'm currently doing initialization based on Objective

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.