Sam
Sam

Reputation: 2331

Swift: "Value of type 'Any' has no member 'map'" with array returned as type Any

I am returning an array from a function which specifies that it's return type is Any. I specify the return type any because sometimes this function does not return an array and this function can return anything. Here is my code

import Foundation

func example() -> Any {
    return ["Example"]
}

func example2() {
    example().map {$0}
}

I am receiving this error

Value of type 'Any' has no member 'map'

How can I get rid of this error and have a function that can return anything including Any and [Any]

Upvotes: 0

Views: 2277

Answers (1)

aiwiguna
aiwiguna

Reputation: 2922

you need to cast it to array

func example2() {
    if let array = example() as? [Any] {
       let map = array.map {$0} 
    }
}

enter image description here

Upvotes: 2

Related Questions