何为协议,何为委托,何为分类(类别)?
委托 即 代理 delegate:
@interface A: UIView
@property(nonatic, retain) id aValueDelegate;
@end;
@implementation A
- (void) fa
{
NSString *value = @"hello";
[aValueDelegate aValue:value];
}
@end;
@interface B: UIView
NSString *value;
@end;
@implementation B
- (void) aValue:(NSString *)fromValue
{
value = fromValue;
NSLog(@"%@", value);
}
@end;
A *a = [[A alloc] init];
B *b = [[B alloc] init];
a.aValueDelegate = b; //设置a代理委托对象为b
@interface NSString(NumberConvenience) -(NSNumber*) lengthAsNumber; @end
@interface NSObject (***Delegate) //method @end
//
// Dog.h
// protocol2
//
// Created by peter on 14-2-25.
// Copyright (c) 2014年 peter. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Dog : NSObject
@property int ID;
@end
@interface NSObject(myCategory)
- (void)callFromNSObject;
@end
//
// Dog.m
// protocol2
//
// Created by peter on 14-2-25.
// Copyright (c) 2014年 peter. All rights reserved.
//
#import "Dog.h"
@implementation Dog
- (id)init
{
self = [super init];
return self;
}
@end
@implementation NSObject(myCategory)
- (void)callFromNSObject
{
NSLog(@"iam nsobject");
}
@end
//
// Person.h
// protocol2
//
// Created by peter on 14-2-25.
// Copyright (c) 2014年 peter. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Dog.h"
@interface Person : NSObject
//{
// Dog *dog;
//}
@property Dog *dog;
- (void)callFun;
@end
//
// Person.m
// protocol2
//
// Created by peter on 14-2-25.
// Copyright (c) 2014年 peter. All rights reserved.
//
#import "Person.h"
@implementation Person
@synthesize dog;
- (void) callFun
{
NSLog(@"Call Fun!");
[dog callFromNSObject];
}
@end
//
// main.m
// protocol2
//
// Created by peter on 14-2-25.
// Copyright (c) 2014年 peter. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Dog.h"
#import "Person.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Dog *dog = [[Dog alloc]init];
[dog setID:10];
Person *qy = [[Person alloc]init];
[qy setDog:dog];
[qy callFun];
}
return 0;
}
@protocol NSCopying
-(id) copyWithZone:(NSZone *) zone;
@end
@interface Car : NSObject<NSCopying>
{
// instance variable
}
// method
@end // Car
@protocol myprotocol <NSObject>
@optional
-(void)print:(int)value;
//可选的方法
@required
-(int)printValue:(int)value1 andValue:(int)value2;
//必须实现的
@end
#import <Foundation/Foundation.h>
#import "myprotocol.h"
//实现协议 myprotocol
@interface mytest : NSObject<myprotocol>
- (void)showInfo;
@end
#import "mytest.h"
@implementation mytest
-(void)showInfo
{
NSLog(@"I am in showInfo");
}
//实现协议必须实现的
-(int)printValue:(int)value1 andValue:(int)value2
{
NSLog(@"print value1 %d,value2 %d",value1,value2);
return 0;
}
//实现可选的
-(void)print:(int)value
{
NSLog(@"print value is %d",value);
}
@end
#import <Foundation/Foundation.h>
#import "mytest.h"
#import "myprotocol.h"
int main (int argc, const char * argv[]) {
@autoreleasepool {
mytest *test=[[mytest alloc]init];
[test showInfo];
[test printValue:20 andValue:30];
//print协议是可选的,所以在用之前一定要判断是否实现了,不然可能会出错,使用下面的方法
// [test print:20];
SEL sel=@selector(print:);
if([test respondsToSelector:sel]){
[test print:11];
}
//用协议的方式实现
id<myprotocol> protocol =[[[mytest alloc]init]autorelease];
[protocol showInfo];
[protocol printValue:200 andValue:300];
if([protocol respondsToSelector:@selector(print:)]){
[protocol print:111];
}
}
return 0;
}//
// Dog.h
// catagory
//
// Created by peter on 14-2-25.
// Copyright (c) 2014年 peter. All rights reserved.
//
#import <Foundation/Foundation.h>
@protocol dogBark;
@interface Dog : NSObject
{
int barkCount;
NSTimer *timer;
}
@property int ID;
@property (assign)id<dogBark> delegate; //dog master
@end
@protocol dogBark <NSObject>
- (void)bark: (Dog*)thisDog count:(int)count;
@end
//
// Dog.m
// catagory
//
// Created by peter on 14-2-25.
// Copyright (c) 2014年 peter. All rights reserved.
//
#import "Dog.h"
@implementation Dog
//ID use _ID
@synthesize delegate;
- (id)init
{
if (self = [super init]) {
//create nstimer user, 1.0s use updateTimer:nil
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
}
return self;
}
- (void) updateTimer:(id)arg
{
barkCount++;
NSLog(@"dog bar %d", barkCount);
//user master delegate bark:count
[delegate bark:self count:barkCount];
}
@end
//
// person.h
// catagory
//
// Created by peter on 14-2-25.
// Copyright (c) 2014年 peter. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Dog.h"
@interface person : NSObject<dogBark>
{
Dog *_dog;
}
@property (retain, nonatomic) Dog *dog;
@end
//
// person.m
// catagory
//
// Created by peter on 14-2-25.
// Copyright (c) 2014年 peter. All rights reserved.
//
#import "person.h"
@implementation person
- (void)setDog:(Dog *)aDog
{
if (_dog != aDog) {
[_dog setDelegate:self];
}
}
//dog use person interface
- (void)bark:(Dog *)thisDog count:(int)count
{
NSLog(@"person bark: this dog %d bark %d", [thisDog ID], count);
}
@end
//
// main.m
// catagory
//
// Created by peter on 14-2-25.
// Copyright (c) 2014年 peter. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Dog.h"
#import "person.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
person *qy = [[person alloc]init];
Dog *dog = [[Dog alloc]init];
[dog setID:10];
[qy setDog:dog];
while (1) {
[[NSRunLoop currentRunLoop]run];
}
}
return 0;
}
【IOS学习】之四、协议,委托,分类粗解,布布扣,bubuko.com
原文:http://blog.csdn.net/jofranks/article/details/19964001