Objective-C:我应该声明私有方法吗?

发布于 2024-11-25 10:47:30 字数 340 浏览 1 评论 0原文

我一直在类扩展中声明私有方法,根据 在 Objective-C 中为类定义私有方法的最佳方式

但是,我刚刚意识到,在 Xcode 4 中,如果我完全省略私有方法的声明并仅实现它,则应用程序编译并运行时不会出现警告或错误。

那么,我是否应该在类扩展中声明私有方法呢?

为什么我们必须声明方法呢?在 Java 中,您不需要...在 Ruby 中也不会。

I've been declaring private methods in class extensions, according to Best way to define private methods for a class in Objective-C.

But, I just realized that, in Xcode 4, if I leave out the declaration of a private method altogether and just implement it, the app compiles and runs without warning or error.

So, should I even bother declaring private methods in class extensions?

Why should we have to declare methods anyway? In Java, you don't... neither in Ruby.

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

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

发布评论

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

评论(3

小兔几 2024-12-02 10:47:30

仅当调用者在方法之前声明时才需要定义方法定义。为了保持一致性,我建议在扩展中定义您的私有方法。

-(void)somemethod
{
}

-(void)callermethod
{
    //No warning because somemethod was implemented already
    [self somemethod];
}

-(void)callermethod2
{
    //Warning here if somemethod2 is not defined in the header or some extension
    [self somemethod2];
}

-(void)somemethod2
{
}

A method definition only needs to be defined if the caller is declared before the method. For consistency I would recommend defining your private methods in the extension.

-(void)somemethod
{
}

-(void)callermethod
{
    //No warning because somemethod was implemented already
    [self somemethod];
}

-(void)callermethod2
{
    //Warning here if somemethod2 is not defined in the header or some extension
    [self somemethod2];
}

-(void)somemethod2
{
}
书信已泛黄 2024-12-02 10:47:30

Joe 在 v4.3 之前已经为 Xcode 正确回答了这个答案。但是,在 v4.3 及更高版本中,不仅不需要声明私有方法,而且声明顺序现在也无关紧要。有关详细信息,请参阅:

Objective-C 中的私有方法,在 Xcode 4.3 中我不再需要在实现文件中声明它们?

This answer has already been correctly answered by Joe for Xcode prior to v4.3. However, in v4.3 and above, not only do private methods not need to be declared, but declaration order is now irrelevant. For details, see:

Private Methods in Objective-C, in Xcode 4.3 I no longer need to declare them in my implementation file ?

辞旧 2024-12-02 10:47:30

这将在没有声明的情况下编译和运行良好:

- (void)foo {
}

- (void)bar {
    [self foo];
}

但最后我检查过,这将给出警告:

- (void)bar {
    [self foo];
}

- (void)foo {
}

换句话说,就像在 C 中一样:如果定义在任何使用之前,则不需要声明。 C 需要这样做,以避免向编译器添加额外的传递(一个用于查找函数,然后一个用于实际解析它们)。至于在不需要时是否应该声明它们,这实际上取决于您正在使用的代码库的风格。

至于其他不需要声明的语言,有些只是进行额外的传递,而另一些则不需要在编译时知道参数的数量和类型或返回类型(它们而是在运行时查找函数) ,或者他们一开始就没有强类型变量,所以它并不“重要”),所以他们可以跳过它。

This will compile and run fine without declaration:

- (void)foo {
}

- (void)bar {
    [self foo];
}

But last I checked, this will give a warning:

- (void)bar {
    [self foo];
}

- (void)foo {
}

In other words, it's just like in C: a declaration is not necessary if the definition comes before any use. C requires this to avoid having to add an extra pass to the compiler (one to find the functions and then one to actually parse them). As for whether you should declare them when not necessary, it's really up to the style of the codebase you're working with.

As for other languages that don't require declarations, some just go ahead with the extra pass, while others don't need to know the number and types of the arguments or the return type at compile time (they look up functions at runtime instead, or they don't have strongly-typed variables to begin with so it doesn't "matter") so they can just skip it.

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