可以通过结构扩展Swift Dictionary吗?

时间:2016-11-23 14:07:52

标签: swift generics struct swift-extensions

我已经在extension of Dictionary where <String, AnyObject>尝试了解决方案,但它不会为我编译。

我只想将字典扩展名限制为struct类型。有没有办法实现这个目标?

import Cocoa

struct Foo: Hashable {
    let bar: String

    static let predefinedFoo = Foo(bar: "something")

    var hashValue: Int { return bar.hashValue }

    public static func ==(lhs: Foo, rhs: Foo) -> Bool {
        return lhs.hashValue == rhs.hashValue
    }
}


struct Baz {
    let isSpecial: Bool
}


extension Dictionary where Key: Foo, Value: Baz { // Note that the == syntax does not compile, either
    var hasSpecialPredefined: Bool {
        return self[.predefinedFoo]?.isSpecial ?? false
    }
}

let test: [Foo: Baz] = [.predefinedFoo: Baz(isSpecial: true)]

test.hasSpecialPredefined

使用上面的代码,我得到两个编译错误:

error: type 'Key' constrained to non-protocol type 'Foo'
error: type 'Value' constrained to non-protocol type 'Baz'
error: '[Foo : Baz]' is not convertible to '<<error type>>'
    test.hasSpecialPredefined
    ^~~~

是否可以通过结构约束扩展?如果不是,为什么不呢?这看起来非常合理。

  

请注意,此处FooBar不受我的控制。它们表示在外部模块中定义的结构,我想要扩展的字典也来自此模块。答案应该假定Foo 总是struct,该结构将always作为字典的密钥类型。

1 个答案:

答案 0 :(得分:1)

试试我的版本

1。代码(带结构)

import Foundation

struct Foo: Hashable {
    let bar: String

    static let predefinedFoo = Foo(bar: "something")

    var hashValue: Int { return bar.hashValue }

    static func ==(lhs: Foo, rhs: Foo) -> Bool {
        return lhs.hashValue == rhs.hashValue
    }
}

struct Baz {
    let isSpecial: Bool
    init(isSpecial: Bool) {
        self.isSpecial = isSpecial
    }
}

extension Dictionary where Key: Any, Value: Any {
    var hasSpecialPredefined: Bool {
        for key in keys {
            if let _key = key as? Foo, _key == .predefinedFoo, let value = self[key] as? Baz {
                return value.isSpecial
            }
        }
        return false
    }
}

let foo1 = Foo(bar: "ddddd")
var test: [Foo: Baz] = [foo1: Baz(isSpecial: true)]
print("\(test), hasSpecialPredefined: \(test.hasSpecialPredefined)")
test[.predefinedFoo] =  Baz(isSpecial: true)
print("\(test), hasSpecialPredefined: \(test.hasSpecialPredefined)")

结果

enter image description here

2。代码(带类)

import Foundation

class Foo: Hashable {
    let bar: String

    init(bar:String) {
        self.bar = bar
    }

    static let predefinedFoo = Foo(bar: "something")

    var hashValue: Int { return bar.hashValue }

    public static func ==(lhs: Foo, rhs: Foo) -> Bool {
        return lhs.hashValue == rhs.hashValue
    }
}

class Baz: AnyObject {
    let isSpecial: Bool
    init(isSpecial: Bool) {
        self.isSpecial = isSpecial
    }
}

extension Dictionary where Key: Foo, Value: Baz {
    var hasSpecialPredefined: Bool {
        for key in keys {
            if key == .predefinedFoo {
                return self[key]?.isSpecial ?? false
            }
        }
        return false
    }
}

let foo1 = Foo(bar: "ddddd")
var test: [Foo: Baz] = [foo1: Baz(isSpecial: true)]
print ("hasSpecialPredefined: \(test.hasSpecialPredefined)")
test[.predefinedFoo] =  Baz(isSpecial: true)
print ("hasSpecialPredefined: \(test.hasSpecialPredefined)")