Using Swift 5.7.2 and Xcode 14.2 I am attempting to write an extension function to an array of a certain type i.e. [MyClass]
. Inside the function I would like to be able to use the Array()
initializer to convert a set to an array, but I am not able to. I get the following error: No exact matches in call to initializer
.
To simulate the problem I created a small playground with the code found below, where I simply try to extend [Int]
. Additionally I realized that this is only a problem when extending an array, as the error does not appear when I extend just the Int
type.
I am super curious about why this happens, and I hope someone can help me figure it out. There is most likely a logical explanation to this.
Extending [Int] (does not work)
extension [Int] {
func foo() {
let strSet = Set(["a", "b", "c", "a"])
let strArray = Array(strSet) // No exact matches in call to initializer
print(strArray)
}
func bar() {
let strSet = Set(["a", "b", "c", "a"])
let strArray = strSet.map {$0}
print(strArray)
}
}
Extending Int (works fine)
extension Int {
func foo() {
let strSet = Set(["a", "b", "c", "a"])
let strArray = Array(strSet) // Works fine
print(strArray)
}
func bar() {
let strSet = Set(["a", "b", "c", "a"])
let strArray = strSet.map {$0}
print(strArray)
}
}
Not an extension (works fine)
func foo() {
let strSet = Set(["a", "b", "c", "a"])
let strArray = Array(strSet)
print(strArray)
}