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)
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)
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)
Just drop the typing of the array...
var array = [alican]
Typs is inferred wherever possible by swift.
You only need to explicitly type a variable if it is not possible to infer it automatically.
This is how you can access name and surname
var alican = (name: "alican", surame:"yilmaz")
var array = [alican]
print(array[0].name)