在Objective-c中,我如何创建所有子类都可以访问但不能公开访问的方法 [英] In Objective-c how do I make a method that's accessible by all child class, but not accessible publicly

查看:96
本文介绍了在Objective-c中,我如何创建所有子类都可以访问但不能公开访问的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我创建一个viewController.然后,我想创建另一个与第一个类似的viewController.看起来像是一项继承工作.

Say i create a viewController. Then I want to create another viewController that's like the first one. Looks like a job for inheritance.

但是,originalViewController的许多任务是私有方法,我想最小化公共接口.

However, many of the task of the originalViewController are private methods and I want to minimize public interface.

如果我将这些方法声明为公共方法,那么,实现私有事物私有化的目标就无法实现.

If I declare those methods public then well, the objective of keeping private things private is not achieved.

如果我将这些方法声明为私有,则子类将不知道这些方法.

If I declare those methods private, then the child class do not know those methods.

如果我为子类创建一个特殊的.h文件,那会很尴尬.

If I create a special .h files for the child class, it'll be awkward.

那么,做这种事情的行业标准方法是什么?

So what's the industry standard way of doing this sort of thing anyway?

在C ++中,我们将使用受保护的方法或函数来实现此权利吗?

In C++ we would use protected methods or function to accomplish this right?

更新:

我放

#import "BGGoogleMap+protected.h"

@implementation BGGoogleMap ()

@end

它不起作用.它要求输入标识符.

It doesn't work. It asks for identifier.

推荐答案

除了Catfish_Man建议的内容外,您还可以在类中创建仅子类可以访问的类别. 此处是文档要做到这一点.基本上,您只需为类创建一个新接口,然后在其中放置任何私有方法.像这样:

In addition to what Catfish_Man suggests, you can make categories on your class that only your subclasses have access to. Here are the docs for how to do that. Basically, you just create a new interface for your class and put any private methods in there. Like this:

@interface MyClass (MyClass_Private)
// Method declarations
@end

将这些内容放入Foo_Internal.h中,并将Foo_Internal.h包含在您的子类中,但不要将其公开.

Put those in Foo_Internal.h, and include Foo_Internal.h in your subclasses, but don't make it public.

这篇关于在Objective-c中,我如何创建所有子类都可以访问但不能公开访问的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆