Home > DeveloperSection > Forums > Block super class method to call sub-class
John Brown
John Brown

Total Post:6

Posted on    November-03-2015 4:49 AM

 iPhone iPhone  Ios  Objective C 

 1 Reply(s)
 578  View(s)
Rate this:

I have some serious code in my app that make our apps superclass method dangerous, whenever I run the app it will hang in a loop in the context of the subclass. I know it is not a good work but I am a beginner in app development so I’ll take time to learn. I our code I need to either block that method form outside or throw an exception whenever it is called directly to the superclass, but still I am using it from sub-class, without caring it.
So, I want the best way to do it.
Here is the code that created problem on my app, In sub-class my override method is like this:

- (int) myMethod
  [NSException raise:@"HangingProblem!"

format:@"Do not use this method to call directly by the sub-class”];
  return nil;

Please give me the suggestions on my code to solve hanging issue.

Tarun Kumar

Total Post:214

Posted on    November-03-2015 10:05 PM

To overcome the problem, use this code in your .h file:

- (int)dangerousMethod __attribute__((unavailable("message")));

Using method like above, whenever anyone using your subclass dangerous method will be unavailable,
To keep other code from using the superclass's version this method, you can further restrict by putting this in your .m file.

-(int) dangerousMethod{
    return nil;    

Don't want to miss updates? Please click the below button!

Follow MindStick