Swift: Type does not conform to 'ArrayLiteralConvertible' protocol
Keep getting error in my playground saying Set is not ArrayLiteralConvertible protocol
struct Set<T: Hashable> : ArrayLiteralConvertible {
typealias Element = T
private var contents: [Element: Bool]
init() {
self.contents = [Element: Bool]()
}
// easier initialization
init<S: SequenceType where S.Generator.Element == Element>(_ sequence: S) {
self.contents = [Element: Bool]()
Swift.map(sequence) { self.contents[$0] = true }
}
// allow Set creation through simple assignment using ArrayLiteralConvertible protocol
internal static func convertFromArrayLiteral(elements: Element...) -> Set {
return Set(elements)
}
}
source to share
ArrayLiteralConvertible
you need to implement a type initializer init(arrayLiteral: Element...)
. Something like this will reuse your initializer, which takes a sequence:
init(arrayLiteral: Element...) {
self = Set(arrayLiteral)
}
If you're doing this in a playground hit opt-cmd-enter to see more details in the editor assistant than just getting an error at the source. It shows the details of all the protocol requirements that you meet.
By the way, if you declared content like this:
private var contents: [Element: Bool] = [:]
you don't need to initialize it in each of your initializers.
source to share
ArrayLiteralConvertible
requires an initializer init(arrayLiteral elements: Element...)
in the class
init(arrayLiteral elements: Element...) {
self = Set(elements)
}
You don't need to use the function anymore convertFromArrayLiteral
. It was fast in the old version. It has been deprecated in the iOS 8.1 API. You just need the initializer above.
https://developer.apple.com/library/ios/releasenotes/General/iOS81APIDiffs/modules/Swift.html
source to share