protocol associatedType Generic

dnrur124·2022년 5월 9일
0

protocol Series

목록 보기
4/5

protocol 사용방법중 또 하나 assosiatedType 와 Generic 개념을 연결해서 한개의 클래스의 프로퍼티를 여러가지 타입을 적용할수있다(Generic) Company 별로 sellingList 를 DigitalStore라는 프로토콜을 따르는 Store 모델에 적용해보겠다


protocol Company {
    init(sellingList: [String])
}

struct Apple: Company {
    var sellingList: [String]
}

struct SamSung: Company {
    var sellingList: [String]
}

struct Hwawei: Company {
    var sellingList: [String]
}



protocol DigitalStore {
    associatedtype CompanyType: Company
    associatedtype ChildStore: DigitalStore where ChildStore.CompanyType == CompanyType
    var sellingList: [CompanyType] { get set }
    var childStores: [ChildStore] { get set }
}

class BigStore<T: Company>: DigitalStore {
    
    var sellingList = [T]()
    var childStores = [SubStore<T>]()
}

class SubStore<T: Company>: DigitalStore {
    
    var sellingList = [T]()
    var childStores = [SubStore<T>]()
}


let SFAppleStore = BigStore<Apple>()

SFAppleStore.sellingList = [Apple(sellingList: ["iMac", "MacBook", "iPad", "iPhone", "AppleWatch", "AirPods", "AppleTag"])]
SFAppleStore.childStores = [SubStore<Apple>(), SubStore<Apple>()]
let GarosugillStore = SFAppleStore.childStores[0]
GarosugillStore.sellingList = [Apple(sellingList: ["iMac", "MacBook", "iPad", "iPhone", "AppleWatch", "AirPods"])]
let FreesBeeStore = SFAppleStore.childStores[1]
FreesBeeStore.sellingList = [Apple(sellingList: ["iPhone", "AppleWatch", "AirPods"])]
print(SFAppleStore.sellingList)
print(SFAppleStore.childStores)
print(GarosugillStore.sellingList)
print(FreesBeeStore.sellingList)

let KRSamsungStore = BigStore<SamSung>()

KRSamsungStore.sellingList = [SamSung(sellingList: ["Galuxy", "GaluxyFold","GaluxyBook","GaluxyTap","Buzz","SamsungMonitor"])]
KRSamsungStore.childStores = [SubStore<SamSung>(), SubStore<SamSung>()]
let SamsungDigitalPraza = KRSamsungStore.childStores[0]
SamsungDigitalPraza.sellingList = [SamSung(sellingList: ["Galuxy", "GaluxyFold","GaluxyBook","GaluxyTap","Buzz"])]
let Himart = KRSamsungStore.childStores[1]
Himart.sellingList = [SamSung(sellingList: ["Galuxy", "GaluxyFold","Buzz"])]
print(KRSamsungStore.sellingList)
print(KRSamsungStore.childStores)
print(SamsungDigitalPraza.sellingList)
print(Himart.sellingList)

//출력값
[protocol.Apple(sellingList: ["iMac", "MacBook", "iPad", "iPhone", "AppleWatch", "AirPods", "AppleTag"])]
[protocol.SubStore<protocol.Apple>, protocol.SubStore<protocol.Apple>]
[protocol.Apple(sellingList: ["iMac", "MacBook", "iPad", "iPhone", "AppleWatch", "AirPods"])]
[protocol.Apple(sellingList: ["iPhone", "AppleWatch", "AirPods"])]
[protocol.SamSung(sellingList: ["Galuxy", "GaluxyFold", "GaluxyBook", "GaluxyTap", "Buzz", "SamsungMonitor"])]
[protocol.SubStore<protocol.SamSung>, protocol.SubStore<protocol.SamSung>]
[protocol.SamSung(sellingList: ["Galuxy", "GaluxyFold", "GaluxyBook", "GaluxyTap", "Buzz"])]
[protocol.SamSung(sellingList: ["Galuxy", "GaluxyFold", "Buzz"])]

protocol의 associatedType을 이용해서 Generic 적용해본것이다..

profile
Swift 예비개발자

0개의 댓글