Swift/Xcode:如何在一个月的每一天播放不同的声音?

Swift/Xcode:如何在一个月的每一天播放不同的声音?,swift,xcode,Swift,Xcode,您好,提前谢谢您的建议 我的简单应用程序有一个按钮,按下时会播放声音。我想让这个按钮在一个月的每一天播放不同的声音。这可能吗?如果是这样,什么代码可以实现这一点 以下是我目前掌握的代码: var musicEffect: AVAudioPlayer = AVAudioPlayer () override func viewDidLoad() { super.viewDidLoad() let musicFile = Bundle.main.path(forResource:

您好,提前谢谢您的建议

我的简单应用程序有一个按钮,按下时会播放声音。我想让这个按钮在一个月的每一天播放不同的声音。这可能吗?如果是这样,什么代码可以实现这一点

以下是我目前掌握的代码:

var musicEffect: AVAudioPlayer = AVAudioPlayer ()

override func viewDidLoad() {

    super.viewDidLoad()

    let musicFile = Bundle.main.path(forResource: "0001", ofType: ".mp3")

    do {
        try musicEffect = AVAudioPlayer (contentsOf: URL (fileURLWithPath: musicFile!))
    }

    catch {
        print(error)
    }
}

override func didReceiveMemoryWarning() {

    super.didReceiveMemoryWarning()
}


@IBAction func playMusic(_ sender: Any) {

    musicEffect.play()
}

再次感谢

通过使用
日历
组件(from:)
方法,您可以从
日期
对象中提取月份(或任何其他日期组件),这将允许您做您想做的事情。

日历功能
组件(u:from:)
将为您提供月份的日期。从那里很容易:

let calendar = Calendar.current

let date = Date()
let dayOfMonth = calendar.component(.day, from: date)  //Get the day of the month as an Int
let filename = String(format: "%04d", dayOfMonth)  //Convert to a string "00xx"
let musicFile = Bundle.main.path(forResource: filename, ofType: ".mp3") //Try to find a sound file

这实际上是两个问题:1)如何计算一个月中的哪一天,2)如何根据输入播放声音。你和哪一个有麻烦D谢谢邓肯的专业知识!