Skip to content

Instantly share code, notes, and snippets.

@eliaskg
Created May 25, 2012 03:32
Show Gist options
  • Select an option

  • Save eliaskg/2785598 to your computer and use it in GitHub Desktop.

Select an option

Save eliaskg/2785598 to your computer and use it in GitHub Desktop.
- (void)initWithName {
if (self = [super initWithDifferentName]) {
// Bla..
}
}
@siuying

siuying commented May 25, 2012

Copy link
Copy Markdown
class MyClass < ObjCClass
  def initWithName
    super.initWithDifferentName()
      # do something
    self
  end
end

@eliaskg

eliaskg commented May 25, 2012

Copy link
Copy Markdown
Author

That what I thought too but this throws the following error:

Terminating app due to uncaught exception 'NoMethodError', reason: 'app_delegate.rb:11:
in `initWithFancyName': super: no superclass method `initWithFancyName' for 
#<SubClass:0x8ba1aa0> (NoMethodError)

Here is the sample project: http://cl.ly/Gt1q

@seanlilmateus

Copy link
Copy Markdown
class MyClass < ObjCClass
  def initWithName
    initWithDifferentName()
     # do something
    self
  end
end

this should work, if you didn’t redefined initWithDifferentName() in our subclass, super will be called,
if you override the initWithDifferentName() method you will need to call super

class MyClass < ObjCClass
  def initWithName
    initWithDifferentName()
     # do something
    self
  end
  def initWithDifferentName()
     super
     # do something else
   end
end

@eliaskg

eliaskg commented May 25, 2012

Copy link
Copy Markdown
Author

I think I got it, it's even easier than I thought:

class SubClass < NSObject
  def initWithName
    init

    # do something

    self
  end 
end

@siuying

siuying commented May 25, 2012

Copy link
Copy Markdown

Thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment