Objective C 内部类

发布于 2024-09-18 03:58:03 字数 933 浏览 6 评论 0原文

我正在将一个项目从 C# 移植到 Objective-C,我想知道如何在 Objective-C 中实现内部类(内部意味着仅在该项目内部可见)。

例如,我有 C# 代码:

public abstract class AbstractBaseClass : AInterface
{
     // methods go here
}

internal class InternalSubclass : AbstractBaseClass
{
     // methods go here
}

这是我迄今为止转换的代码:

// AbstractBaseClass.h
#import <Foundation/Foundation.h>
#import "AInterface.h"

@interface AbstractBaseClass : NSObject<AInterface>

// methods go here

@end

// AbstractBaseClass.m
#import "AbstractBaseClass.h"

@implementation AbstractBaseClass

-(void) abstractMethod
{
      [NSException raise:@"abstract method" format:@"This method is abstract, and thus cannot be called"];
}

// more methods

@end

Where should I put theInterfaces and Implements of InternalSubclasses?它们应该位于名为 InternalClasses.h/m 的单独文件中吗?或者我应该没有这些文件的标头而只为它们提供一个 .m 文件。

任何帮助将不胜感激!

I am porting a project from C# to Objective-C, and I would like to know how to implement an Internal class in Objective-C (Internal meaning only visible inside of this project).

For example, I have the code in C#:

public abstract class AbstractBaseClass : AInterface
{
     // methods go here
}

internal class InternalSubclass : AbstractBaseClass
{
     // methods go here
}

This Is the code I have converted so far:

// AbstractBaseClass.h
#import <Foundation/Foundation.h>
#import "AInterface.h"

@interface AbstractBaseClass : NSObject<AInterface>

// methods go here

@end

// AbstractBaseClass.m
#import "AbstractBaseClass.h"

@implementation AbstractBaseClass

-(void) abstractMethod
{
      [NSException raise:@"abstract method" format:@"This method is abstract, and thus cannot be called"];
}

// more methods

@end

Where should I put the interfaces and implementations of InternalSubclasses? Should they be in a separate file called InternalClasses.h/m? Or should I just not have a header for those files and just have a .m file for them.

Any help would be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我三岁 2024-09-25 03:58:03

我通常的做法是在基类的 .m 文件中声明内部类的接口和实现。

当然,如果类变大,您可能需要将它们分开以避免文件太大。如果您正在编写一个框架,则有一种机制可以指定哪些标头公开(即导出到框架的标头目录),否则,您只需求助于文档。

The way I normally do it is to declare the interface and implementation of the internal class inside the .m file for the base class.

Of course, if the classes get big, you might need to separate them out to avoid files that are too big. If you are writing a framework, there is a mechanism to specify which headers get made public (i.e. exported to the framework's headers directory), otherwise, you just have to resort to documentation.

心如荒岛 2024-09-25 03:58:03

是的,真正拥有“内部”类的唯一方法是隐藏它们的标头。但即便如此,如果任何人知道它的名称“NSClassFromString”,那么使用这些类也是微不足道的。我想说你可能想考虑重新构建它,而不是使用 Obj-C 并不真正的类设计风格为.

在不了解您正在做什么的情况下,我无法建议不同的设计。

Yes, the only way to really have 'internal' classes would be to hide the headers for them. But even then, it is trivial for anyone to use those classes if they know the name of it `NSClassFromString' I would say you may want to think about re architecting it instead of using a class design style that Obj-C isn't really made for.

Without knowing more about what you are doing I can't suggest a different design though.

陈年往事 2024-09-25 03:58:03

Obj-C 中同样没有“内部”的概念。正如 Joshua 所说,您能做的最好的事情就是将“内部”子类的标头保留为私有,仅记录基类的接口。

就创建抽象基类而言,您的设计很好。 Apple 将这种设计用于“类簇”,在这种设计中,您实际上并没有得到 NSString 或 NSImage 等东西,而是得到了具有以下性质的其他东西:相同的界面。

在 Obj-C 中实现此目的的另一种方法是使用正式协议:

@protocol AbstractInterface
- (void)method1;
...
@end

然后,您不再传递 AbstractBaseClass *,而是传递 id。如果您的基类具有零状态并且没有默认实现,则它们是等效的。

要解决您的其他问题:每个类都应该有自己的 .h 和 .m 文件。无论特定环境如何,这几乎总是正确的。

There is no concept of "internal" in the same way in Obj-C. As Joshua says, the best you can do is just keep the headers for the "internal" subclasses private, documenting only the interface for the base class.

Your design is fine as far as creating an abstract base class. This design is used by Apple for "class clusters", where you're not really getting a, say, NSString or NSImage, you're getting something else that has the same interface.

The other way to accomplish this in Obj-C is by using a formal protocol:

@protocol AbstractInterface
- (void)method1;
...
@end

Then instead of passing around AbstractBaseClass *s, you pass around id<AbstractInterface>s. If your base class has zero state and no default implementations, they're equivalent.

To address your other question: each class should have its own .h and .m file. This is pretty much always true regardless of this particular context.

懵少女 2024-09-25 03:58:03

感谢您的所有回答,我决定将课程公开,因为我将是目前唯一使用该库的人。我真的只是想知道我是否错过了 Objective C 语言文档中有关内部类的内容,因为您可以将变量标记为 @package

Thank you for all your answers, I have decided to make the classes public, as I will be the only one using this library at this time. I was really just wondering if I had missed something in the documentation of the Objective C language about internal classes, because you can mark variables as @package.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文