Swift:使这个长代码更短

时间:2014-10-06 02:57:10

标签: swift

我想在Swift制作我的第一个应用程序,这是钢琴应用程序。 而且我能以某种方式成功!

但我想知道如何缩短代码。 我可能不得不使用数组和循环(?)等东西,并自己搜索但无法得到它。

如果有人可以提供帮助,我真的很感激。

这是我的"查看controller.swift"

import UIKit
import AVFoundation


class ViewController: UIViewController {

var pianoSoundC3 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("C3", ofType: "mp3")!)
var audioPlayerC3 = AVAudioPlayer()

var pianoSoundCS = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("C#", ofType: "mp3")!)
var audioPlayerCS = AVAudioPlayer()

var pianoSoundD = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("D", ofType: "mp3")!)
var audioPlayerD = AVAudioPlayer()

var pianoSoundDS = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("D#", ofType: "mp3")!)
var audioPlayerDS = AVAudioPlayer()

var pianoSoundE = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("E", ofType: "mp3")!)
var audioPlayerE = AVAudioPlayer()

var pianoSoundF = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("F", ofType: "mp3")!)
var audioPlayerF = AVAudioPlayer()

var pianoSoundFS = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("F#", ofType: "mp3")!)
var audioPlayerFS = AVAudioPlayer()

var pianoSoundG = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("G", ofType: "mp3")!)
var audioPlayerG = AVAudioPlayer()

var pianoSoundGS = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("G#", ofType: "mp3")!)
var audioPlayerGS = AVAudioPlayer()

var pianoSoundA = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("A", ofType: "mp3")!)
var audioPlayerA = AVAudioPlayer()

var pianoSoundAS = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("A#", ofType: "mp3")!)
var audioPlayerAS = AVAudioPlayer()

var pianoSoundB = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("B", ofType: "mp3")!)
var audioPlayerB = AVAudioPlayer()

var pianoSoundC4 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("C4", ofType: "mp3")!)
var audioPlayerC4 = AVAudioPlayer()


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    audioPlayerC3 = AVAudioPlayer(contentsOfURL: pianoSoundC3, error: nil)
    audioPlayerC3.prepareToPlay()

    audioPlayerCS = AVAudioPlayer(contentsOfURL: pianoSoundCS, error: nil)
    audioPlayerCS.prepareToPlay()

    audioPlayerD = AVAudioPlayer(contentsOfURL: pianoSoundD, error: nil)
    audioPlayerD.prepareToPlay()

    audioPlayerDS = AVAudioPlayer(contentsOfURL: pianoSoundDS, error: nil)
    audioPlayerDS.prepareToPlay()

    audioPlayerE = AVAudioPlayer(contentsOfURL: pianoSoundE, error: nil)
    audioPlayerE.prepareToPlay()

    audioPlayerF = AVAudioPlayer(contentsOfURL: pianoSoundF, error: nil)
    audioPlayerF.prepareToPlay()

    audioPlayerFS = AVAudioPlayer(contentsOfURL: pianoSoundFS, error: nil)
    audioPlayerFS.prepareToPlay()

    audioPlayerG = AVAudioPlayer(contentsOfURL: pianoSoundG, error: nil)
    audioPlayerG.prepareToPlay()

    audioPlayerGS = AVAudioPlayer(contentsOfURL: pianoSoundGS, error: nil)
    audioPlayerGS.prepareToPlay()

    audioPlayerA = AVAudioPlayer(contentsOfURL: pianoSoundA, error: nil)
    audioPlayerA.prepareToPlay()

    audioPlayerAS = AVAudioPlayer(contentsOfURL: pianoSoundAS, error: nil)
    audioPlayerAS.prepareToPlay()

    audioPlayerB = AVAudioPlayer(contentsOfURL: pianoSoundB, error: nil)
    audioPlayerB.prepareToPlay()

    audioPlayerC4 = AVAudioPlayer(contentsOfURL: pianoSoundC4, error: nil)
    audioPlayerC4.prepareToPlay()

}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


@IBAction func C3(sender: UIButton) {
    audioPlayerC3.currentTime = 0
    audioPlayerC3.play()
}

@IBAction func CS(sender: UIButton) {
    audioPlayerCS.currentTime = 0
    audioPlayerCS.play()
}

@IBAction func D(sender: UIButton) {
    audioPlayerD.currentTime = 0
    audioPlayerD.play()
}

@IBAction func DS(sender: UIButton) {
    audioPlayerDS.currentTime = 0
    audioPlayerDS.play()
}

@IBAction func E(sender: UIButton) {
    audioPlayerE.currentTime = 0
    audioPlayerE.play()
}

@IBAction func F(sender: UIButton) {
    audioPlayerF.currentTime = 0
    audioPlayerF.play()
}

@IBAction func FS(sender: UIButton) {
    audioPlayerFS.currentTime = 0
    audioPlayerFS.play()
}

@IBAction func G(sender: UIButton) {
    audioPlayerG.currentTime = 0
    audioPlayerG.play()
}

@IBAction func GS(sender: UIButton) {
    audioPlayerGS.currentTime = 0
    audioPlayerGS.play()
}

@IBAction func A(sender: UIButton) {
    audioPlayerA.currentTime = 0
    audioPlayerA.play()
}

@IBAction func AS(sender: UIButton) {
    audioPlayerAS.currentTime = 0
    audioPlayerAS.play()
}

@IBAction func B(sender: UIButton) {
    audioPlayerB.currentTime = 0
    audioPlayerB.play()
}

@IBAction func C4(sender: UIButton) {
    audioPlayerC4.currentTime = 0
    audioPlayerC4.play()
}

}

0 个答案:

没有答案