Skip to content

Instantly share code, notes, and snippets.

@honjo2
Created May 23, 2011 10:21
Show Gist options
  • Save honjo2/986507 to your computer and use it in GitHub Desktop.
Save honjo2/986507 to your computer and use it in GitHub Desktop.
[Objective-C] プロトコル
#import <Foundation/Foundation.h>
#import "MapDelegate.h"
@interface LightMapDelegate : NSObject <MapDelegate> {
}
@end
#import "LightMapDelegate.h"
@implementation LightMapDelegate
-(int)getMaxZoomLevel {
return 12;
}
@end
#import <UIKit/UIKit.h>
#import "Map.h"
#import "LightMapDelegate.h"
int main(int argc, char *argv[]) {
id<MapDelegate> delegate = [[LightMapDelegate alloc] init];
Map *map = [[Map alloc] initWithMapDelegate:delegate];
NSString * maxLevelString = [map getMaxLevelString];
NSLog(maxLevelString);
}
#import <Foundation/Foundation.h>
#import "MapDelegate.h"
@interface Map : NSObject {
id delegate;
}
@property(assign) id<MapDelegate> delegate;
-(id)initWithMapDelegate:(id <MapDelegate>)_delegate;
-(NSString *)getMaxLevelString;
@end
#import "Map.h"
#import "MapDelegate.h"
@implementation Map
@synthesize delegate;
-(id)initWithMapDelegate:(id <MapDelegate>)_delegate {
self = [super init];
if (self != nil) {
delegate = _delegate;
}
return self;
}
-(NSString *)getMaxLevelString {
int level = [delegate getMaxZoomLevel];
NSString *ret = [NSString stringWithFormat:@"最大ズームレベルは%dです。", level];
return ret;
}
@end
#import <UIKit/UIKit.h>
@protocol MapDelegate
@required
-(int)getMaxZoomLevel;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment