gpt4 book ai didi

ios - Swift/Spring - 对成员 'subscript' 的引用不明确

转载 作者:行者123 更新时间:2023-11-30 13:05:45 25 4
gpt4 key购买 nike

我的第二个问题又与 Spring 有关,非常感谢您的帮助。

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

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)
}

}

最佳答案

缓存目录定义为

static var cache = [NSURL: SystemSoundID]()

即键的类型为 NSURL。但是 Bundle.main.url(...) 返回URL,它是“对应的”值类型。你可以从 URLNSURL 的桥接:

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

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

static var cache = [URL: SystemSoundID]()

关于ios - Swift/Spring - 对成员 'subscript' 的引用不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39398204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com