Class Extensions

Class Extensions

A class extension is a nameless category that exists solely as an interface section, like this:

@interface MyClass ()

// stuff goes here

@end

Typically, the only classes that will be permitted to “see” a class extension will be the class that’s being extended or a subclass of that class. If only the former is the case, the class extension will usually appear directly in the class’s implementation (.m) file, like this:

// MyClass.m:


@interface MyClass ()

// stuff goes here

@end


@implementation MyClass {

    // ivars

}

// methods

@end

That’s such a common arrangement that Xcode’s project template files actually give you a class extension in certain classes. For example, our Empty Window project comes with a class extension at the start ofViewController.m — take a look and see!

What on earth sort of “stuff” could possibly go into a class extension to make it so useful that it appears in a template file? First, I’ll tell you what a class extensionused to be used for: it was the standard solution to the problem of method definition order.

Before the Objective-C language improvements introduced by LLVM compiler version 3.1 (Xcode 4.3), one method in an implementation section couldn’t call another method in that same implementation section unless either the definition or a method declaration for that other method preceded it. It’s finicky work trying to arrange all the method definitions in the right order, so the obvious solution is a method declaration. A method declaration can go only into an interface section. But to put a method declaration into the interface section in this class’s header file is annoying — it means we must switch to another file — and, even worse, it makes that method public; any class that imports that header file can now see and call this method. That’s fine if this method is supposed to be public; but what if we wanted to keep it private? The solution: a class extension at the start of this class’s implementation file. Put the method declarations into that class extension; all the methods in the implementation section can now see those method declarations and can call one another, but no other class can see them.

Nowadays, though, that trick is unnecessary: methods (and functions) in a class implementation can see and call one another regardless of order.

In modern Objective-C, the usefulness of class extensions has to do mostly with property declarations (which I don’t discuss untilChapter 12). Property declarations, like method declarations, must appear in an interface section; and, although some properties are intended to be public, it is also often the case that we would prefer certain properties to remain private to the class — they are global to all methods of the class, and are useful for preserving values and permitting those values to be accessed by multiple methods, but no other class can see them. The solution is to declare the private properties in a class extension.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值