エンジニアになりたい文系大学生

プログラミングを始めてからもうすぐ1年。iOSアプリ開発が中心。最近Webサービスの開発をやりたいのでRuby on Railsに触れ始めてます。

【iOS】デリゲート

デリゲートの設定

今回は完全に忘れないためのメモ。

Objective-C

Sample.h

#import <UIKit>

@class Sample;

@protocol SampleDelegate <NSObject>

- (void)sampleDelegateMethod;

@end

@interface Sample : NSObject

@property (nonatomic, weak) id<SampleDelegate>delegate;

@end

Sample.m

[self.delegate sampleDelegateMethod];

Swift

sample.swift

protocol SampleDelegate: class {
    func sampleDelegateMethod()
}

class Sample: NSObject {
    weak var delegate: SampleDelegate? = nil

    func callDelegateMethod() {
        delegate?.sampleDelegateMethod()
    }
}

最近のプロジェクトではSwiftを使ってるのですが、Swift好きになりますね。
まだ重かったりするみたいですけど、早くメインになってほしいかもしれないです。
Swiftの"?"とか"!"についてまだよくわかっていないので調べないとです...