在类扩展或@implementation块中添加伪私有ivars有什么区别? [英] What's the difference between adding pseudo-private ivars in a class extension or in the @implementation block?

查看:84
本文介绍了在类扩展或@implementation块中添加伪私有ivars有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将伪私有实例变量放在.m文件中的类扩展中,或者将它们放在新引入的@implementation括号中有什么区别?如下所示?

What's the difference between putting pseudo-private instance variables in a class extension inside the .m file, or putting them in the newly introduced @implementation brackets like shown below?

是否有后果,优点,缺点是一种或另一种方式?在程序员必须关注的内容中,internal2的处理方式与internal3不同吗? (当然麦克凯会说会有所不同,但问题是如果你在练习与否)。

Are there consequences, pros, cons over one or the other way? Is internal2 treated differently than internal3 in a way a programmer must care of? (of course there is a difference McKay would say but the question is if you care in practice or not).

// MyClass.m

@interface MyClass () {
    id internal2;
}
@end


@implementation MyClass {
    id internal3;
}

- (void)internalMethod {
    NSLog(@"%@ %@", internal2, internal3);
}

@end

来源:http://www.mcubedsw.com/blog/index.php/site/comments/new_objective-c_features /

推荐答案

这两种方法的主要区别在于您可以将类扩展包含在单独的标题中,而@implementation ivars显然必须与.m文件中的@implementation块一起使用(并且对于给定的类只能有一个@implementation(不包括扩展名))。实际结果是你可以有多个级别的私人ivars:

The main difference between the two approaches is that you can include the class extension in a separate header, whereas the @implementation ivars obviously have to go with the @implementation block in the .m file (and there can only be one @implementation for a given class (extensions not included)). The practical result of this is that you can have multiple levels of "private" ivars:


  • MyClass.h:public ivars

  • MyClass + Private.h:半私人ivars

  • MyClass.m:非常私人的ivars

作为一个假设的例子,假装MyClass是UIView。在这种情况下,UIView.h是我们都可以访问的标题,UIView + Private.h是只有Apple可以访问的私有标题,而UIView.m只有那些专门负责UIView的人需要知道的东西关于。

As a hypothetical example, pretend that MyClass is UIView. In that case, UIView.h is the header that we can all access, UIView+Private.h is the "private" header than only Apple can access, and UIView.m has stuff that only the people specifically responsible for UIView need to know about.

这篇关于在类扩展或@implementation块中添加伪私有ivars有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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