Skip to content

Instantly share code, notes, and snippets.

@greggjaskiewicz
Created May 1, 2017 14:40
Show Gist options
  • Save greggjaskiewicz/335cd8fb82031712aa7a676c02160c23 to your computer and use it in GitHub Desktop.
Save greggjaskiewicz/335cd8fb82031712aa7a676c02160c23 to your computer and use it in GitHub Desktop.
twoStructsAnyArray_associatedTypes
struct TypeOneStruct {
    let foo1: Int
    let foo2: Int
    let bar1: Bool
}
struct TypeTwoStruct {
    let fooString: String
    let barBool: Bool
    let foo1: Int
}
let a = TypeOneStruct(foo1: 1, foo2: 2, bar1: false)
let b = TypeOneStruct(foo1: 2, foo2: 3, bar1: true)
let c = TypeTwoStruct(fooString: "two", barBool: false, foo1: 2)
let d = TypeTwoStruct(fooString: "d", barBool: true, foo1: 5)
let array: [Any] = [a,b,c,d, "ello"]
for element in array {
    if let one = element as? TypeOneStruct {
        print(one.bar1, one.foo1, one.foo2)
    } else if let two = element as? TypeTwoStruct {
        print(two.barBool, two.foo1, two.fooString)
    } else {
        print("idk")
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment