1
首先,我是Swift/iOS
開發新手,剛開始學習。我試圖創建一個簡單的應用程序,播放/暫停音樂。該界面有一個播放和暫停按鈕,以及用於控制音樂音量的滑塊。我搜索過並發現了一些類似的帖子,但沒有發現它們對我的情況有幫助。Xcode 6.1與Swift:終止應用程序,由於未捕獲異常'NSUnknownKeyException'
當我建立並開始在Xcode的應用程序,我得到了錯誤:
2015-04-07 23:04:25.403 Music Player[8772:102170] *** Terminating app due
to uncaught exception 'NSUnknownKeyException', reason:
'[<Music_Player.ViewController 0x7f991ad3a160> setValue:forUndefinedKey:]:
this class is not key value coding-compliant for the key play.'
*** First throw call stack:
(
0 CoreFoundation 0x000000010a45bf35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010bf9fbb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010a45bb79 -[NSException raise] + 9
3 Foundation 0x000000010a8737b3 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 259
4 CoreFoundation 0x000000010a3a5e80 -[NSArray makeObjectsPerformSelector:] + 224
5 UIKit 0x000000010afacc7d -[UINib instantiateWithOwner:options:] + 1506
.....
22 UIKit 0x000000010ace7420 UIApplicationMain + 1282
23 Music Player 0x0000000109f24afe top_level_code + 78
24 Music Player 0x0000000109f24b3a main + 42
25 libdyld.dylib 0x000000010c779145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
我使用AVAudioPlayer類實例化一個播放器播放和暫停音樂。
var player: AVAudioPlayer = AVAudioPlayer()
@IBAction func play(sender: AnyObject) {
var audioPath = NSString(string: NSBundle.mainBundle().pathForResource("music", ofType: "mp3")!) //file: music.mp3
var error: NSError? = nil
//instantiate the player
player = AVAudioPlayer(contentsOfURL: NSURL(string: audioPath), error: &error) //error pointer
player.prepareToPlay()
player.play()
}
@IBAction func pause(sender: AnyObject) {
player.pause()
}
我沒有比下面的代碼之外的滑塊做任何事情:
@IBOutlet var slider: UISlider!
@IBAction func sliderChanged(sender: AnyObject) {
}
在GUI下面附:
檢查故事板中的每個插座。他們中的任何一個人都指向一個不存在的人。 – itsji10dra
可能的重複[這是什麼意思? 「'NSUnknownKeyException',原因:...這個類不是密鑰X的編碼兼容鍵值」](http://stackoverflow.com/questions/3088059/what-does-this-mean-nsunknownkeyexception-reason-該級 - 是 - 不鍵) – jtbandes