[swift-users] Initializing an empty dictionary

Adriano Ferreira adriano.ferreira at me.com
Wed Mar 9 10:45:08 CST 2016


Hi everyone,

Does anyone know why Swift won’t let me use the alternative way below to init the dictionary?

public extension SequenceType where Self.Generator.Element: Hashable {

    @warn_unused_result
    func frequencies() -> [Self.Generator.Element: Int] {
        var dictionary: [Self.Generator.Element: Int] = [:]
        // var dictionary: Dictionary<Self.Generator.Element, Int>() // <<< Not allowed!

        for element in self {
            dictionary[element] = dictionary[element]?.successor() ?? 1
        }

        return dictionary
    }
}

I’m using Swift 2.1 and Xcode 7.2.

Best,

— A
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160309/191ceadf/attachment.html>


More information about the swift-users mailing list