開発をスピードアップするために何度も再利用できるライブラリを作成し、必要に応じて新しいプロジェクトに追加したいと考えています。基本的には、抽象化レイヤーを構築したいと考えています。誰かがそれを行う方法と、コードのこの部分の理由を教えてもらえますか:
if ([self.delegate respondsToSelector:@selector(enableCamera)]) {
BOOL enabled;
enabled = [self.delegate enableCamera];
if (enabled == YES) {
[self enableCameraMethod];
}
呼ばれませんか?
以下は私のコードです:
ライブラリ.h:
@protocol usesCamera <NSObject>
@optional
-(BOOL)enableCamera;
@end
@interface Library : NSObject
@property (nonatomic, weak) id <usesCamera> delegate;
-(void)enableCameraMethod;
@end
library.m
#import "Library.h"
@implementation Library
- (id) init
{
if (self = [super init]) {
if ([self.delegate respondsToSelector:@selector(enableCamera)]) {
BOOL enabled;
enabled = [self.delegate enableCamera];
if (enabled == YES) {
[self enableCameraMethod];
}
}
return (self);
}
}
-(void)enableCameraMethod {
NSLog(@"Implement my camera method here");
}
@end
UIViewController.h
#import <UIKit/UIKit.h>
#import "Library.h"
@interface ViewController : UIViewController <usesCamera>
@end
UIViewController.m
#import "ViewController.h"
#import "Library.h"
@interface ViewController ()
@property (nonatomic, strong) UIViewController *myVC;
@end
@implementation ViewController
-(BOOL)enableCamera {
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
Library *myLibrary = [[Library alloc] init];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end