Skip to content

Instantly share code, notes, and snippets.

@eliaskg
Created May 25, 2012 03:32
Show Gist options
  • Save eliaskg/2785598 to your computer and use it in GitHub Desktop.
Save eliaskg/2785598 to your computer and use it in GitHub Desktop.
- (void)initWithName {
if (self = [super initWithDifferentName]) {
// Bla..
}
}
@siuying
Copy link

siuying commented May 25, 2012

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

@eliaskg
Copy link
Author

eliaskg commented May 25, 2012

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

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
Copy link
Author

eliaskg commented May 25, 2012

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

class SubClass < NSObject
  def initWithName
    init

    # do something

    self
  end 
end

@siuying
Copy link

siuying commented May 25, 2012

Thank you :)

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