Alican Yilmaz Alican Yilmaz - 2 years ago 115
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]

Answer Source

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) })
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download