Skip to content

Instantly share code, notes, and snippets.

@TuenTuenna
Last active September 3, 2021 07:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TuenTuenna/86af9eba50ee0a05922091b5725c5f3c to your computer and use it in GitHub Desktop.
Save TuenTuenna/86af9eba50ee0a05922091b5725c5f3c to your computer and use it in GitHub Desktop.
protocol

SwiftUi 프로토콜로 제네릭 처리

import UIKit

protocol Test {
    var one: Int { get set }
    var two: Int { get set }
}

struct Test1 : Test {
    var one: Int
    var two: Int
}

struct Test2 : Test {
    var one: Int
    var two: Int
}

let testArr1 = [Test1(one: 1, two: 2), Test1(one: 3, two: 4)]

let testArr2 = [Test2(one: 1, two: 2), Test2(one: 3, two: 4)]


func testGeneric<T>(_ dataArray: [T]) {
    if let list = dataArray as? [Test] {
        print("\(list[0].one)")
    }
}


testGeneric(testArr1)

testGeneric(testArr2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment