首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在swift中声明变量时可以使用通配符吗?

在swift中声明变量时可以使用通配符吗?
EN

Stack Overflow用户
提问于 2018-06-09 21:46:42
回答 1查看 169关注 0票数 -1

我正在通过构建一个Soundboard应用程序来学习编码。我正在使用audioEngine播放一些声音,在我改变了一些曲调之后。在我的viewDidLoad中,我声明我的variables

代码语言:javascript
复制
var audioFile1 = AVAudioFile()
var audioFile2 = AVAudioFile()

if let filePath = Bundle.main.path(forResource: "PeteNope", ofType:
        "mp3") {
        let filePathURL = URL(fileURLWithPath: filePath)

        setPlayerFile(filePathURL)

    }
if let filePath2 = Bundle.main.path(forResource: "Law_WOW", ofType:
        "mp3") {
        let filePath2URL = URL(fileURLWithPath: filePath2)

        setPlayerFile2(filePath2URL)

    }
func setPlayerFile(_ fileURL: URL) {
    do {
        let file1 = try AVAudioFile(forReading: fileURL)

        self.audioFile1 = file1


    } catch {
        fatalError("Could not create AVAudioFile instance. error: \(error).")
    }
}

func setPlayerFile2(_ fileURL: URL) {
    do {
        let file2 = try AVAudioFile(forReading: fileURL)

        self.audioFile2 = file2


    } catch {
        fatalError("Could not create AVAudioFile instance. error: \(error).")
    }
}

然后,我按如下方式连接nodes

代码语言:javascript
复制
audioEngine.attach(pitchPlayer)
audioEngine.attach(timePitch)
audioEngine.connect(pitchPlayer, to: timePitch, format: audioFile1.processingFormat)
audioEngine.connect(timePitch, to: audioEngine.outputNode, format: audioFile1.processingFormat)
audioEngine.connect(pitchPlayer, to: timePitch, format: audioFile2.processingFormat)
audioEngine.connect(timePitch, to: audioEngine.outputNode, format: audioFile2.processingFormat) 

所以,我的问题是,由于变量除了一个数字之外都是相同的,有没有一种方法可以通过编程来编写它,这样我就不必单独声明nodes了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-10 00:56:57

根据我对您问题的理解,您有许多"PeterNope“和"Law_WOW”类型的资源,并且希望快速地将它们添加为节点。

您可以按如下方式重构代码,以添加任意数量的资源:

代码语言:javascript
复制
var resources = ["PeterNope", "Law_WOW", "otherResource1", "otherResource2", ...]

func setPlayerFile(_ fileName: String) {
    // make sure resource exists and break if not.
    guard let filePath = Bundle.main.path(forResource: fileName, ofType: "mp3") else {
        print ("resource file \(fileName) does not exist")            
        return
    }

    let fileURL = URL(fileURLWithPath: filePath)

    // open file from resource
    do {
        let file = try AVAudioFile(forReading: fileURL)
    } catch {
        fatalError("Could not create AVAudioFile instance. error: \(error).")
    }

    // connect nodes
    connect(pitchPlayer, to: timePitch, format: file.processingFormat)
    audioEngine.connect(timePitch, to: audioEngine.outputNode, format: file.processingFormat)
}

// connect all resources
attach(pitchPlayer)
audioEngine.attach(timePitch)

for index in 0..(resources.count - 1) {
    setPlayer(resources[index]
}

// or quicker and swiftier:
resources.forEach { setPlayerFile($0) }

我推荐阅读for循环和控制流的一般内容:https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50774934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档