大佬教程收集整理的这篇文章主要介绍了ios – 如何在目标c中覆盖超类中的私有方法和属性,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
ClassInLibrary.h
@interface ClassInLibrary : UIView - (void)publicMethod; @end
ClassInLibrary.m
@interface ClassInLibrary () @property BOOL privateBoolean; @end @implementation ClassInLibrary - (void)privateMethod { ... } - (void)publicMethod { ... self.privateBoolean = YES; [self privateMethod]; } @end
我想要做的是创建一个ClassInLibrary的子类并覆盖publicMethod.但是,由于aBoolean和privateMethod不可见,有没有办法覆盖publicMethod,因为我需要使用它们?
Subclass.m
@interface Subclass () @end @implementation Subclass - (void)publicMethod { ... self.privateBoolean = NO; // cAnnot access [self privateMethod]; // cAnnot access } @end
编辑:
我同意@Anoop Vaidya,调用私有API不是一个好主意.真实案例是公共方法中的硬编码字符串,我想修改它.
Superclass.m
- (void)publicMethod { ... [self privateMethod]; ... NSString *String = @"a String"; [self doSomeThingWithTheString]; // private ... }
我不能使用[super publicMethod],因为它会再次调用privateMethod.
可能有更好,更安全的方法来实现这一目标吗?
您可以使用选择器调用私有方法,但它会显示一个警告,您可以禁用它.
SEL SELEctor = NSSELEctorFromString(@"privateMethod"); if ([a respondsToSELEctor:SELEctor]) { NSLog(@"yes privateMethod exists"); #pragma clang diagnostic push #pragma clang diagnostic ignored "-Warc-performSELEctor-leaks" [self performSELEctor:SELEctor]; #pragma clang diagnostic pop }
以上是大佬教程为你收集整理的ios – 如何在目标c中覆盖超类中的私有方法和属性全部内容,希望文章能够帮你解决ios – 如何在目标c中覆盖超类中的私有方法和属性所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。