Alican Yilmaz Alican Yilmaz - 6 months ago 12
Swift Question

Swift: Accessing members of tuples in Array

Why am i getting this error?
Error: Value of type 'Any' (aka protocol<>) has no member 'name'

import UIKit
var alican = (name: "alican", surame:"yilmaz")
var array:[Any] = [alican]
print(array[0].name)

Answer

You've declared your Array as containing Any type. If you declare it like this the error should go away:

var array:[(name: String, surame: String)] = [alican]

If the array needs to be able to contain Any type you can pull out just those matching a particular type using flatMap.

var array:[Any] = [alican]
var nameSurnames = array.flatMap({ return $0 as? (name: String, surame: String) })
print(nameSurnames[0].name)