IOS4 note 11 (2)

Private Method Declarations

 

A problem arises when you’d like to declare a method in such a way that all other methods in the same class can see the declaration (and can thus call the method) without putting that declaration in the class’s interface section where every other class that imports the header file will also be able to see and call it.

 

The  solution  is  to put  an  interface  section  for  a  category on  your own  class  in  the implementation file, which no one imports (Example 10-1).

Example 10-1. Declaring a method privately

// [in MyClass.m]

#import "MyClass.h"

@interface MyClass (Tricky)

- (void) myMethod;

@end

 


@implementation MyClass

// all methods here can call myMethod

@end

 

This  trick  cannot  completely  prevent  some  other  class  from  calling  this  class’s myMethod — Objective-C  is too dynamic for that — but at  least a normal call to myMethod from some other class will get its hand slapped by the compiler.

In Example 10-1, the compiler will not warn if the methods declared in the category interface section (such as myMethod) are not defined in the implementation section. If this worries you, there are two solutions. One is to provide a named category implementation section, corresponding to the named category interface section; if you fail to implement the category-declared methods in this category implementation section, the compiler will warn:

@implementation MyClass (Tricky)

// must implement myMethod here, or compiler will warn

@end

 

The other approach is just the opposite, namely to remove the category name altogether from the category implementation section; if you then fail to implement the categorydeclared methods in the normal implementation section, the compiler will warn:

@interface MyClass ()

- (void) myMethod;

@end

This nameless type of category is called a class extension.

 

 

 

Protocols

A protocol is just a named list of method declarations, with no implementation. A class may formally declare that it conforms to (or adopts) a protocol; such conformance is inherited by subclasses. This declaration satisfies the compiler when you try to send a corresponding message.

A protocol method may be required or optional.

A protocol is just a named list of method declarations, with no implementation. A class may formally declare that it conforms to (or adopts) a protocol; such conformance is inherited by subclasses.

Here’s how the NSCopying protocol is defined (in NSObject.h, where your code can see it):

@protocol NSCopying

- (id)copyWithZone:(NSZone *)zone;

@end

That’s all there is to defining a protocol. The definition uses the @protocol compiler directive; it states the name of the protocol; it consists entirely of method declarations; and it is terminated by the @end compiler directive. A protocol definition will typically appear in a header file, so that classes that need to know about it (in order to call its methods) can  import  it. A @protocol section of a header  file  is not  inside any other section (such as an @interface section).

 

  Conform formally to the NSCopying protocol

@interface MyClass : NSObject <NSCopying>

Example of how Cocoa uses protocols

@property (nonatomic, assign) id<UITableViewDataSource> dataSource

 

 

Checking if object responds to some method, you can call respondsToSelector: but  it slows things down a  little. 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值