2

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..."].

5
  • What does the declaration of SwiftyStoreKit.retrieveProductsInfo() look like? Commented Sep 17, 2016 at 1:03
  • @RemyLebeau its a function that accepts an array of product ids. When I have the array ["Monthly","Yearly"] hard typed in it works no problem. Trying to make it dynamic passing an array through my app. Commented Sep 17, 2016 at 1:05
  • that is not what I asked. What is the actual declaration of retrieveProductsInfo()? Clearly it expects something other than what you are giving it, or else you wouldn't be getting the error. Commented Sep 17, 2016 at 1:06
  • 1
    func getProductInfo(productIDs: Set<String> Commented Sep 17, 2016 at 1:08
  • 1
    @LeoDabus You are the best. I can accept you answer if you'd like to "answer the question". Mind explaining why Set was the right answer? Ty! Commented Sep 17, 2016 at 1:15

3 Answers 3

2

["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

    ...
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

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>)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.