2016-09-17 85 views
1

在Swift中,我有一个函数将一个数组传递给数组,然后在另一个函数中使用该数组。我不断收到此错误:无法将类型'Array [String]'的值转换为期望的参数类型'Set <String>'

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 

    ... 

其余的工作,当我通过在["Monthly", "Yearly", "etc..."]规则阵列进行测试。

+0

什么'SwiftyStoreKit.retrieveProductsInfo()'的声明看起来像? –

+0

@RemyLebeau是一个接受产品ID的数组的函数。当我有阵列[“每月”,“每年”]硬输入它没有问题。试图通过我的应用动态地传递一个数组。 – Dan

+0

这不是我问的。 'retrieveProductsInfo()'的**实际**声明是什么?很显然,它期望的不是你给的东西,否则你不会得到错误。 –

回答

1

你只需要改变你的方法参数类型。 SwiftyStoreKit方法期待一个字符串集。您的方法声明应该是:

func getProductInfo(productIDs: Set<String>) 
2

["Monthly", "Yearly", "etc..."]不是一个数组,它是一个数组字面量。 Set可以用数组文字隐式初始化。但是,它不能用数组隐式地初始化。

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

但是,如果你明确地初始化它,那么它将工作。

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

因此,在你的例子显式初始化应该工作。

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

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

    ... 
1

我使用相同的lib面临问题。 这应该工作 SwiftyStoreKit.retrieveProductsInfo(Set(productIDs))

相关问题