下位互換コード隠蔽のストイシズム

Post on 10-May-2015

8.781 Views

Category:

Technology

4 Downloads

Preview:

Click to see full reader

DESCRIPTION

サンプルコード: https://github.com/taketo1024/iOS6CompatibilizerDemo

TRANSCRIPT

下位互換コード隠蔽のストイシズム

@taketo1024

14年1月15日水曜日

14年1月15日水曜日

@implementation ViewController

- (void)viewDidLoad{ [super viewDidLoad]; self.edgesForExtendedLayout = UIRectEdgeAll; self.automaticallyAdjustsScrollViewInsets = NO;}

.

.

.

@end

14年1月15日水曜日

14年1月15日水曜日

14年1月15日水曜日

iOS 6 互換

14年1月15日水曜日

if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {

self.edgesForExtendedLayout = UIRectEdgeAll; self.automaticallyAdjustsScrollViewInsets = NO;

}

14年1月15日水曜日

if([[[UIDevice currentDevice] systemVersion] compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending) {

self.edgesForExtendedLayout = UIRectEdgeAll; self.automaticallyAdjustsScrollViewInsets = NO;

}

14年1月15日水曜日

if(iOSVersionOver(@"7.0")) { self.edgesForExtendedLayout = UIRectEdgeAll; self.automaticallyAdjustsScrollViewInsets = NO;}

14年1月15日水曜日

分岐処理は美しくない

14年1月15日水曜日

アプリ本体のコードからOS分岐処理を消したい

14年1月15日水曜日

方針1: 拡張カテゴリで

iOS 6 互換メソッドを追加

14年1月15日水曜日

@interface UIViewController(iOS6Compatibilized)

@property (nonatomic) UIRectEdge iOS6_edgesForExtendedLayout;

@property (nonatomic) BOOL iOS6_automaticallyAdjustsScrollViewInsets;

@end

UIViewController+iOS6Compaibilized.h14年1月15日水曜日

@implementation UIViewController (iOS6Compatibilized)

- (UIRectEdge)iOS6_edgesForExtendedLayout{ if(iOSVersionOver(@"7.0")) { return self.edgesForExtendedLayout; } else { return UIRectEdgeAll; }}

.

.

.

UIViewController+iOS6Compaibilized.m14年1月15日水曜日

@implementation MyViewController

- (void)viewDidLoad{ [super viewDidLoad]; self.iOS6_edgesForExtendedLayout = UIRectEdgeAll; self.iOS6_automaticallyAdjustsScrollViewInsets = NO;}

MyViewController.m14年1月15日水曜日

イマイチ美しくない

14年1月15日水曜日

@implementation MyViewController

- (void)viewDidLoad{ [super viewDidLoad]; self.iOS6_edgesForExtendedLayout = UIRectEdgeAll; self.iOS6_automaticallyAdjustsScrollViewInsets = NO;}

MyViewController.m14年1月15日水曜日

@implementation MyViewController

- (void)viewDidLoad{ [super viewDidLoad]; self.edgesForExtendedLayout = UIRectEdgeAll; self.automaticallyAdjustsScrollViewInsets = NO;}

MyViewController.m14年1月15日水曜日

方針2: UIViewControllerサブクラスを作る

14年1月15日水曜日

@interface UIViewController_iOS6 : UIViewController

@end

UIViewController_iOS6.h14年1月15日水曜日

- (UIRectEdge)edgesForExtendedLayout{ if(iOSVersionOver(@"7.0")) { return super.edgesForExtendedLayout; } else { return UIRectEdgeAll; }}

.

.

.

UIViewController_iOS6.m14年1月15日水曜日

#import "UIViewController_iOS6.h"

@interface MyViewController : UIViewController_iOS6

@end

MyViewController.h14年1月15日水曜日

@implementation MyViewController

- (void)viewDidLoad{ [super viewDidLoad]; self.edgesForExtendedLayout = UIRectEdgeAll; self.automaticallyAdjustsScrollViewInsets = NO;}

MyViewController.m14年1月15日水曜日

必ずコレを継承しなきゃいけないのは嫌

14年1月15日水曜日

アプリ本体のコードには全く手を付けないのがストイック

14年1月15日水曜日

@interface MyViewController : UIViewController

@end

MyViewController.h14年1月15日水曜日

@implementation MyViewController

- (void)viewDidLoad{ [super viewDidLoad]; self.edgesForExtendedLayout = UIRectEdgeAll; self.automaticallyAdjustsScrollViewInsets = NO;}

MyViewController.m14年1月15日水曜日

方針3: 拡張カテゴリ

+動的メソッド追加

14年1月15日水曜日

@interface UIViewController(iOS6Compatibilized)

@end

UIViewController+iOS6Compaibilized.h14年1月15日水曜日

@implementation UIViewController (iOS6Compatibilized)

- (UIRectEdge)iOS6_edgesForExtendedLayout{ return UIRectEdgeAll;}

- (void)iOS6_setEdgesForExtendedLayout:(UIRectEdge)edge{ // do nothing}

(続く)

UIViewController+iOS6Compaibilized.m14年1月15日水曜日

(続き)

- (BOOL)iOS6_automaticallyAdjustsScrollViewInsets{ return NO;}

- (void)iOS6_setAutomaticallyAdjustsScrollViewInsets:(BOOL)automaticallyAdjustsScrollViewInsets{ // do nothing}

(続く)

UIViewController+iOS6Compaibilized.m14年1月15日水曜日

(続き)

+ (void)compatibilizeInstanceMethodOfSelector:(SEL)selector withSelector:(SEL)compatible{ Method method = class_getInstanceMethod(self, compatible);

class_addMethod(self, selector, method_getImplementation(method), method_getTypeEncoding(method));

}

(続く)

UIViewController+iOS6Compaibilized.m14年1月15日水曜日

+ (void)load{ [self compatibilizeInstanceMethodOfSelector:@selector(edgesForExtendedLayout) withSelector:@selector(iOS6_edgesForExtendedLayout)];

[self compatibilizeInstanceMethodOfSelector:@selector(setEdgesForExtendedLayout:) withSelector:@selector(iOS6_setEdgesForExtendedLayout:)];

[self compatibilizeInstanceMethodOfSelector:@selector(automaticallyAdjustsScrollViewInsets) withSelector:@selector(iOS6_automaticallyAdjustsScrollViewInsets)];

[self compatibilizeInstanceMethodOfSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:) withSelector:@selector(iOS6_setAutomaticallyAdjustsScrollViewInsets:)];

}

@end

UIViewController+iOS6Compaibilized.m14年1月15日水曜日

ストイックである。

14年1月15日水曜日

補足:class_addMethod() は

既にメソッドが存在する場合は何もしない

14年1月15日水曜日

補足:+(void)load は

拡張カテゴリ毎に呼ばれる

14年1月15日水曜日

iOS 7 で追加されたクラス/メソッドを全部作り直せば、iOS 7 のコードのま

ま iOS 6 対応ができる。

14年1月15日水曜日

どこまでストイックにやるかはあなた次第。

14年1月15日水曜日

Thank you.@taketo1024

14年1月15日水曜日

top related