Objective-C 接口编程

1. @Protocal (协议)post

    @protocal 是Objective-C 中广泛存在的接口定义方式,即在一个类中经过@protocal 中定义接口,在另外的类中实现该接口,这种定义方式夜晨为"delegate"模式。@protocal 声明了能够被其余任何类实现的方法,协议仅仅是定义一个接口,由其余类区负责实现。
ui

    "delegate"模式的使用共分为三个步骤:
atom

        1)接口声明:建立 一个protocal 文件TestDelegatespa

            @protocal TestDelegate<NSObject>
.net

            @required
线程

                -(void)doSomething;
server

            @end            对象

        2)接口回调:定义一个类A,包含一个听从此协议的属性CC,并定义一个方法用调用CC的方法,这样当实例化A的对象时,就能够经过delegate回调了。接口

            @interface  TestAppDelegate:NSObject事件

            {

                 id <TestDelegate> delegate;

            }

            @property (retain) id delegate;

            @end

            @implementation TestAppDelegate

            -(void)doSomethingWithProtocal

            {

               [self.delegate doSomething];

            }

            @end 

        3)借口实现:建立一个类B实现协议<TestDelegate>

            @interface  TestAppDelegate:NSObject<TestDelegate>

            @end

            @implementation TestAppDelegate

            -(void)doSomething

            {

                NSLog(@"call protocol method");

            }

            @end 

    注意:因为B的对象是A的delegate,则若是B要release了,则应该先将A的delegate置为nil,而后再releaseB的对象。

2. NSNotifications (通知)

    Notifications 是 iOS 提供的一种“同步的”消息通知机制,即消息在发送到下一个接收者以前须要前一个接收者快速返回,不然就是阻塞下一个接收者接收消息,因此对于长时间的消息处理,须要另外启动一个线程来单独处理消息接收后的任务,以确保消息接收到后能够当即返回。

    这种消息通知机制能够应用于任意事件和对象,具备广播的性质,消息发送发送者对消息接收者能够一无所知。观察者能够有多个,只要向消息中心注册便可接收其余对象发送的消息,当不须要接收消息时,须要向消息中心注销。

    这种消息广播机制时典型的“Observer”模式,应用这种模式须要5个步骤。

    1)定义消息

        //A.h        extern NSString * const NOTIFICATION_STR;

        //A.m       NSString * const NOTIFICATION_STR  = @"notificationStr";        

        或者在 工程的 myObjective-Prefix.pch 文件中定义

        #define  NOTIFICATION_STR  @"notificationStr"

    2)发送消息

        // A.m

        [[NSNOtificationCenter defaultCenter] postNotificationName: NOTIFICATION_STR object:nil];

        注意:这里的object 能够是你定义的一个对象,能够用来传值给消息接收者

        还有另外两种发送方式: 

[[NSNotificationCenter defaultCenter]postNotification:(NSNotification *)];

    [[NSNotificationCenter defaultCenter]postNotificationName:(NSString *) object:(id) userInfo:(NSDictionary *)];


    3)观察者注册

        //B.m         [[NSNOtificationCenter defaultCenter] addObserver:self selector:@selector(doSomething:) name:NOTIFICATION_STR object:nil];

    4)观察者处理消息

        //B.m   -(void)doSomething:(NSNotification *)notify

                    {

                        id obj = [notify object];

                        .........

                    }

    5)观察者注销

        // B.m          [[NSNOtificationCenter defaultCenter] removeObserver:self];

3. categories 

    Objective-C 并无真正的似有方法,可是能够经过 Categories 编写私有方法和变量,使他们不暴露给使用者。这也可称做以静态库的形式将代码进行封装。使用有三个步骤:

    1)定义私有Categories

        //SomeClass + hidden.h

        @interface SomeClass (hidden)

        +(void)hiddenMethod;

        -(void)hiddenInstanceMethod;

        @end

    2)实现私有Categories

         //SomeClass + hidden.m

            +(void)hiddenMethod{........}

            -(void)hiddenInstanceMethod{........}

    3)主类调用私有方法

        // SomeClass.m

        @implemetation SomeClass

            -(void)doSomeThing

            {

                [self hiddenMethod];

                [instance hiddenInstanceMethod];

            }

        @end

附:其实经常使用的不少OC对象的方法都是 定义在 categories中的,如NSString除了 length 和 getCharaterAtIndex: 外基本上都是定义在categories中的。 NSData也同样 等。


4. @protocal 封装类

    在Obj-c中 categories 中一样能够实现 @protocal 定义的接口。这样就能够为接口提供封装类,在封装类中实现接口定义的功能,类扩展实现接口的语法。须要三个步骤:

    1)定义接口protocal

        @protocal SomeDelgate<NSObject>

        -(void)doSomething;

        @end

    2)定义类 和 听从SomeDelegate协议的扩展类

        //SomeClass.h

        @interface SomeClass:NSObject

        @property (nonatomic,assign) id <SomeDelegate> delegate;

        @end

        //SomeClass + CategoriesClass.h

        #import SomeClass.h

        @interface SomeClass(CategoriesClass) <SomeDelegate>

        @end

        //SomeClass + CategoriesClass.m

        @implemetation SomeClass(CategoriesClass)

            -(void)doSomething

            {

                NSLog(@"categories implement  protocolMethod.");

            }

        @end

    3) 接口调用

        // SomeClass.m

         @implemetation SomeClass

            SomeClass *obj =  [[SomeClass alloc]init];

            [obj doSomething];

        @end

相关文章
相关标签/搜索