Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Genericsとは
- //クラスや関数を使う側が扱う型を指定できるようにする仕組み
- //これにより、抽象的なコードを記述することを可能にする
- import UIKit
- //Generic Functionsについて
- //============================
- //配列系
- func repeat2<ItemType2:Sequence>(item: ItemType2, times: Int) -> [ItemType2] {
- var result = [ItemType2]()
- for _ in 0...times {
- result.append(item)
- }
- return result
- }
- repeat2(item: "uhoho", times: 10)
- repeat2(item: [10,11], times: 3)
- //============================
- //共通プロトコル準拠系
- //protocolにプロパティも設定できるんやね
- protocol prTest {
- var testp:Int? { get set }
- func test()
- }
- class ppp1: UIView, prTest {
- var testp:Int?
- func test(){
- print("havetest1")
- }
- }
- class ppp2: NSObject, prTest {
- var testp:Int?
- func test(){
- print("havetest2")
- }
- }
- func dokanya<PppType:prTest>(ttt:PppType){
- ttt.test()
- }
- let nnn = ppp1(frame: CGRect.zero)
- let mmm = ppp2()
- dokanya(ttt: nnn)
- dokanya(ttt: mmm)
- //============================
- //比較可能系
- func eeq<EqualType:Equatable>(kensho:EqualType,kensho2:EqualType) -> Bool{
- return kensho == kensho2
- }
- eeq(kensho: 10, kensho2: 10)
- eeq(kensho: "mmm", kensho2: "mm")
- //============================
Add Comment
Please, Sign In to add comment