我對Swift和iOS編程場景非常陌生,我正在嘗試學習一堆東西。對於這個應用程序,我想保存並加載一個整數,但是我遇到了問題,因爲我在堆棧溢出和Internet上發現的所有內容似乎都無法在Swift 3上運行。該應用程序立即崩潰,斷開連接:在Swift 3.0中保存和加載時遇到問題iOS 10
@IBOutlet weak var StaticLabel: UILabel!
它說「線程1:斷點3.5」。任何幫助和提示將非常讚賞:)
import UIKit
import AVFoundation
var player: AVAudioPlayer?
var number = 0
class ViewController: UIViewController {
@IBOutlet weak var StaticLabel: UILabel!
@IBOutlet weak var NumberLabel: UILabel!
let defaults = UserDefaults.standard
override func viewDidLoad() {
super.viewDidLoad()
Load()
}
func Load()
{
number = defaults.integer(forKey: "Save")
NumberLabel.text = String(number)
}
func playSound()
{
let path = Bundle.main.path(forResource: "RightYouAre.mp3", ofType: nil)
let url = URL(fileURLWithPath: path!)
do {
let sound = try AVAudioPlayer(contentsOf: url)
player = sound
sound.play()
}
catch {
// couldn't load file :(
}
}
@IBAction func Minus(_ sender: AnyObject) {
number = number - 1
NumberLabel.text = String(number)
defaults.set(number, forKey: "Save")
}
@IBAction func Plus(_ sender: UIButton) {
playSound()
number = number + 1
NumberLabel.text = String(number)
defaults.set(number, forKey: "Save")
}
}
你需要更新你的問題(不發表評論)與有關崩潰的細節。什麼是完整的錯誤信息?哪條線確實導致了崩潰? – rmaddy
另請參閱http://www.raywenderlich.com/10209/my-app-crashed-now-what-part-1以瞭解如何調試崩潰。 – rmaddy
您的代碼完全不顯示StaticLabel的任何用法。 –