Objective-C类扩展 [英] Objective-C class extension

查看:106
本文介绍了Objective-C类扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个相当新的Objective-C程序员(具有4年的Java经验),我似乎很难理解何时使用类扩展.从我的理解(如果我错了,请纠正我),类别和扩展之间的主要区别在于扩展希望您在主实现内实现方法,而对于类别,则可以在另一实现内实现.似乎人们也主要将扩展用于私有方法.

As a fairly new objective-c programmer (with a 4 years Java experience), I seem to be having a hard time understanding when to use class extensions. From what I understood (and please, correct me if I'm wrong), the main difference between categories and extensions is that the extension expects you to implement the methods inside your main implementation, whereas with a category, it can be in another implementation. It also seems that people are using extensions mainly for private methods.

这是我的第一个问题.使用类扩展声明私有方法与根本不声明私有方法有什么区别(这两种情况似乎都可以在运行时进行编译)? (示例1与2)

Here's my first question. What's the difference between using a class extension to declare a private method, and not declare it at all (it seems to compile in run in both cases)? (example 1 vs 2)

示例1

@interface Class()
-(void) bar;
@end

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

-(void) bar {
    NSLog(@"bar");
}
@end

示例2

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

-(void) bar {
    NSLog(@"bar");
}
@end

第二个问题:在扩展内部声明ivars与在实现内部直接声明ivars有什么区别? (示例3对4)

Second question: What's the difference between declaring ivars inside the extension and declaring it directly inside the implementation? (Exemple 3 vs 4)

示例3

@interface Class() {
    NSArray *mySortedArray;
}
@end

@implementation Class
@end

示例4

@implementation Class
NSArray *mySortedArray;
@end

关于编码约定,我还有最后一个问题:什么时候应该在变量名称前加下划线(_)?

I have a last question about coding conventions: when should I put an underscore (_) in front of a variable's name?

谢谢

推荐答案

类扩展中的方法

以前从来不需要不需要声明私有方法的情况.直到最近,您还需要在某个地方声明您的私有方法,并且大多数人都选择了类扩展来声明.从Xcode 4.4(我相信)开始,编译器很聪明,可以确定哪些方法在该实现中是私有的,而无需在其他地方声明它们.

It never used to be the case that you didn't need to declare your private methods. Until recently, you needed to declare your private methods somewhere, and most people chose a class extension to do so. From Xcode 4.4 (I believe), the compiler is clever enough to determine which methods are meant to be private within that implementation, removing the need to declare them elsewhere.

课程扩展中的变量

对于示例3和4,要小心.在类扩展中,变量是该类的实例变量(示例3).示例4声明了一个全局变量(由于它遵循C语言中的全局变量语义).坚持使用示例3作为私有实例变量.

As for examples 3 and 4, be careful. Within a class extension, the variable is an instance variable to that class (example 3). Example 4 declares a global variable (due to the fact it follows global variable semantics from C). Stick with example 3 for your private instance variables.

编码约定

对于编码约定,由开发人员/团队决定是否使用下划线.我们的团队将m_用于私有实例变量.苹果在其中文档建议使用下划线(这是综合属性的基础实例变量的命名方式).重要的是,在整个代码中保持一致.

As for the coding convention, it's up to the developer/team to decide whether to use an underscore or not. Our team uses m_ for private instance variables. Apple in their documentation suggest using underscores (that's the naming style for the underlying instance variables for synthesized properties). The important thing is, to be consistent throughout your code.

这篇关于Objective-C类扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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