Guest User

Untitled

a guest
Oct 17th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. //Genericsとは
  2. //クラスや関数を使う側が扱う型を指定できるようにする仕組み
  3. //これにより、抽象的なコードを記述することを可能にする
  4.  
  5. import UIKit
  6.  
  7. //Generic Functionsについて
  8. //============================
  9. //配列系
  10. func repeat2<ItemType2:Sequence>(item: ItemType2, times: Int) -> [ItemType2] {
  11. var result = [ItemType2]()
  12. for _ in 0...times {
  13. result.append(item)
  14. }
  15. return result
  16. }
  17.  
  18. repeat2(item: "uhoho", times: 10)
  19. repeat2(item: [10,11], times: 3)
  20.  
  21. //============================
  22. //共通プロトコル準拠系
  23. //protocolにプロパティも設定できるんやね
  24. protocol prTest {
  25. var testp:Int? { get set }
  26. func test()
  27.  
  28. }
  29.  
  30. class ppp1: UIView, prTest {
  31. var testp:Int?
  32. func test(){
  33. print("havetest1")
  34. }
  35. }
  36.  
  37. class ppp2: NSObject, prTest {
  38. var testp:Int?
  39. func test(){
  40. print("havetest2")
  41. }
  42. }
  43.  
  44. func dokanya<PppType:prTest>(ttt:PppType){
  45. ttt.test()
  46. }
  47.  
  48. let nnn = ppp1(frame: CGRect.zero)
  49. let mmm = ppp2()
  50.  
  51. dokanya(ttt: nnn)
  52. dokanya(ttt: mmm)
  53.  
  54. //============================
  55. //比較可能系
  56. func eeq<EqualType:Equatable>(kensho:EqualType,kensho2:EqualType) -> Bool{
  57. return kensho == kensho2
  58. }
  59.  
  60. eeq(kensho: 10, kensho2: 10)
  61. eeq(kensho: "mmm", kensho2: "mm")
  62.  
  63. //============================
Add Comment
Please, Sign In to add comment