ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Swift] 제네릭 (Generics)
    iOS/Swift 2022. 12. 2. 11:47
    import UIKit
    
    
    struct MyArray<SomeElement>{ //보통 SomeElement 자리에 T를 넣음 "Type"
        // 제네릭을 담은 빈 배열
        var elements : [SomeElement] = [SomeElement]()
        
        //생성자
        init(_ elements: [SomeElement]){
            self.elements = elements
        }
    }
    
    struct Friend {
        var name: String
    }
    
    struct Coder {
        var name: String
    }
    
    var mySomeArray = MyArray([1,2,3])
    print("mySomeArray : \(mySomeArray)")
    
    var myStringArray = MyArray(["가","나","다"])
    print("myStringArray : \(myStringArray)")
    
    let friend_01 = Friend(name: "철수")
    let friend_02 = Friend(name: "영희")
    let friend_03 = Friend(name: "수진")
    
    var myFriendArray = MyArray([friend_01, friend_02, friend_03])
    print("\(myFriendArray)")

    출력결과

    mySomeArray : MyArray<Int>(elements: [1, 2, 3])
    myStringArray : MyArray<String>(elements: ["가", "나", "다"])
    MyArray<Friend>(elements: [__lldb_expr_58.Friend(name: "철수"), __lldb_expr_58.Friend(name: "영희"), __lldb_expr_58.Friend(name: "수진")])

    'iOS > Swift' 카테고리의 다른 글

    [Swift] 상속  (0) 2022.12.04
    [Swift] 객체 생성자, 해제자  (0) 2022.12.04
    [Swift] 메소드 (Methods)  (0) 2022.12.02
    [Swift] 클래스와 스트럭트  (0) 2022.12.01
    [Swift] enum 열거형  (0) 2022.12.01

    댓글

Today
Designed by Danbee Park.