Swift / Spring - 对成员'下标'

时间:2016-09-08 18:50:03

标签: ios swift

我在这里的第二个问题再次与春天有关,非常感谢你的帮助。

以下是接收问题代码的代码行:

var soundID : SystemSoundID = Internal.cache[url] ?? 0

Internal.cache[url] = soundID

问题代码:

Ambiguous reference to member 'subscript'

附加代码:

struct SoundPlayer {

static var filename : String?
static var enabled : Bool = true

private struct Internal {
    static var cache = [NSURL:SystemSoundID]()
}

static func playSound(soundFile: String) {

    if !enabled {
        return
    }

    if let url = Bundle.main.url(forResource: soundFile, withExtension: nil) {

        var soundID : SystemSoundID = Internal.cache[url] ?? 0

        if soundID == 0 {
            AudioServicesCreateSystemSoundID(url as CFURL, &soundID)
            Internal.cache[url] = soundID
        }

        AudioServicesPlaySystemSound(soundID)

    } else {
        print("Could not find sound file name `\(soundFile)`")
    }
}

static func play(file: String) {
    self.playSound(soundFile: file)
}

}

1 个答案:

答案 0 :(得分:0)

缓存目录定义为

static var cache = [NSURL: SystemSoundID]()

即。密钥属于NSURL类型。但是Bundle.main.url(...)会返回 一个URL,它是“对应的”值类型。你也可以 从URLNSURL的桥梁:

 var soundID = Internal.cache[url as NSURL] ?? 0

或(在我看来这是更好的解决方案),改变 字典定义

static var cache = [URL: SystemSoundID]()