Dan
Dan

Reputation: 1063

Cannot convert value of type 'Array[String]' to expected argument type 'Set<String>'

In Swift, I have a function that I am passing an array to, and then using that array in another function. I keep getting this error:

Cannot convert value of type 'Array[String]' to expected argument type 'Set<String>'

@objc func getProductInfo(productIDs: Array<String>) -> Void {
    print(productIDs) //this works with correct data

    SwiftyStoreKit.retrieveProductsInfo(productIDs) { result in

    ...

The rest works, and is tested when I pass in a regular array of ["Monthly", "Yearly", "etc..."].

Upvotes: 2

Views: 4147

Answers (3)

Vyacheslav
Vyacheslav

Reputation: 27211

I've face the issue using the same lib. This should work SwiftyStoreKit.retrieveProductsInfo(Set(productIDs))

Upvotes: 2

Leo Dabus
Leo Dabus

Reputation: 236360

You just need to change you method parameter type. SwiftyStoreKit method is expecting a String Set. Your method declaration should be:

func getProductInfo(productIDs: Set<String>)

Upvotes: 1

Jeffery Thomas
Jeffery Thomas

Reputation: 42588

["Monthly", "Yearly", "etc..."] is not an array, it's an array literal. Set can be implicitly initialized with an array literal.

let ayeSet: Set<String> = ["a"] // Compiles

But, it cannot be implicitly initialized with an array.

let bees: Array<String> = ["b"]
let beeSet: Set<String> = bees // Causes Compiler Error

However, if you explicitly initialize it, then it will work.

let sees: Array<String> = ["c"]
let seeSet: Set<String> = Set(sees) // Compiles

So, in your example explicitly initialization should work.

@objc func getProductInfo(productIDs: Array<String>) -> Void {
    print(productIDs) //this works with correct data

    SwiftyStoreKit.retrieveProductsInfo(Set(productIDs)) { result in

    ...

Upvotes: 2

Related Questions