创建播放声音的按钮,得到NSException错误
问题描述:
import UIKit
import AVFoundation
import AudioToolbox
class ViewController: UIViewController {
var player : AVAudioPlayer?
func playSound(){
let path = Bundle.main.path(forResource: "Roblox", ofType:"mp3")!
let url = URL(fileURLWithPath: path)
do {
let sound = try AVAudioPlayer(contentsOf: url)
self.player = sound
sound.numberOfLoops = 1
sound.prepareToPlay()
sound.play()
} catch {
print("error loading file")
// couldn't load file :(
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func button(_ sender: Any) {
playSound()
}
}
您好,我正在尝试制作播放声音的按钮(Roblox.mp3)。它编译没有错误。但是,当我打开模拟器时,它会崩溃。我的错误日志是:创建播放声音的按钮,得到NSException错误
2017-08-10 20:52:57.876 Oof[2500:252563] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Oof.ViewController 0x7fc320607800> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key button.'
*** First throw call stack:
(
0 CoreFoundation 0x000000010dcd6b0b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010aa8a141 objc_exception_throw + 48
2 CoreFoundation 0x000000010dcd6a59 -[NSException raise] + 9
3 Foundation 0x000000010a5a000b -[NSObject(NSKeyValueCoding) setValue:forKey:] + 292
4 UIKit 0x000000010b0f7994 -[UIViewController setValue:forKey:] + 87
5 UIKit 0x000000010b364a09 -[UIRuntimeOutletConnection connect] + 109
6 CoreFoundation 0x000000010dc7ce8d -[NSArray makeObjectsPerformSelector:] + 269
7 UIKit 0x000000010b3633bf -[UINib instantiateWithOwner:options:] + 1856
8 UIKit 0x000000010b0fdfc3 -[UIViewController _loadViewFromNibNamed:bundle:] + 381
9 UIKit 0x000000010b0fe8d9 -[UIViewController loadView] + 177
10 UIKit 0x000000010b0fec0a -[UIViewController loadViewIfRequired] + 195
11 UIKit 0x000000010b0ff45a -[UIViewController view] + 27
12 UIKit 0x000000010afc798a -[UIWindow addRootViewControllerViewIfPossible] + 65
13 UIKit 0x000000010afc8070 -[UIWindow _setHidden:forced:] + 294
14 UIKit 0x000000010afdaebe -[UIWindow makeKeyAndVisible] + 42
15 UIKit 0x000000010af5437f -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4346
16 UIKit 0x000000010af5a5e4 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1709
17 UIKit 0x000000010af577f3 -[UIApplication workspaceDidEndTransaction:] + 182
18 FrontBoardServices 0x00000001111765f6 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
19 FrontBoardServices 0x000000011117646d -[FBSSerialQueue _performNext] + 186
20 FrontBoardServices 0x00000001111767f6 -[FBSSerialQueue _performNextFromRunLoopSource] + 45
21 CoreFoundation 0x000000010dc7cc01 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
27 Oof 0x000000010a1429b7 main + 55
28 libdyld.dylib 0x0000000110b2d65d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
我真的不确定发生了什么问题。我相信我想要的声音被导入到Xcode中,因为我将它拖放到我的项目中。如果错误是在请求声音的那一行,我可能没有正确格式化它。我已经将它看作@“mp3”和typeOf(“mp3”)。再次,如果这看起来很简单,对不起。我刚开始的时候很快,而且还有一些我正在努力挣扎的语言障碍。
异常原因_这个类不是关键值编码兼容键**按钮** _建议你的'ViewController'没有名为'button'的'@ IBOutlet',但故事板有一个连接到它。这通常发生在您完成一些连接后编辑源代码时。检查所有故事板连接,并将'@ IBOutlet'连接(不是'@ IBAction'连接)删除到'button'。用网上的错误信息搜索,你可以找到很多文章,其中一些文章详细描述_how to_。 – OOPer
https://*.com/questions/24393495/playing-a-sound-with-avaudioplayer请尝试检查此链接,如果它适合你 –