致命错误:init(coder:) 尚未实现 THEN Property 'self.album'未在 super.init 调用中初始化 THEN 为自身分配属性
因此,我正在创建一个应用程序,其中包含一个音频播放器,一旦用户登录其帐户,就可以通过从主页调用的按钮访问该音频播放器。应用程序的所有页面都是使用 Storyboard 创建的,但音频播放器是使用 SwiftUI 以编程方式完成的。该应用程序运行良好,直到您按下按钮访问音频播放器。然后我在 AudioPlayerViewController 上收到“线程 1:致命错误:init(coder:) 尚未实现”。所以我在网上搜索了一个解决方案,并根据这里的建议,在 stackOverflow 上,删除了这个:
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
并按照建议放置了这个:
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
但后来我收到此错误:
“属性'self.album'未在 super.init 调用时初始化”
所以我添加:“ self.album = album” - 如下所示:
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.album = album
}
但随后我收到错误:“为自身分配属性”
现在我累了。发送帮助。
让我知道我是否应该分享我的代码或其他任何内容的链接,我以前没有在这里问过问题。
我想这就是完整的代码:
import UIKit
final class AudioPlayerViewController: UIViewController {
var album: Album
private lazy var mediaPlayer: MediaPlayer = {
let v = MediaPlayer(album: album)
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
init(album: Album) {
self.album = album
super.init(nibName: nil, bundle: nil)
}
// required init?(coder: NSCoder) {
// fatalError("init(coder:) has not been implemented")
// }
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.album = album
}
// override func awakeFromNib() {
// super.awakeFromNib()
// //custom logic goes here
// }
//
override func viewDidLoad() {
super.viewDidLoad()
setupView()
}
private func setupView() {
addBlurredView()
view.addSubview(mediaPlayer)
setupConstraints()
}
private func addBlurredView() {
if !UIAccessibility.isReduceTransparencyEnabled {
self.view.backgroundColor = UIColor.clear
let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.dark)
let blueEffectView = UIVisualEffectView(effect: blurEffect)
blueEffectView.frame = self.view.bounds
blueEffectView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
view.addSubview(blueEffectView)
} else {
view.backgroundColor = UIColor.black
}
}
private func setupConstraints() {
NSLayoutConstraint.activate([
mediaPlayer.leadingAnchor.constraint(equalTo: view.leadingAnchor),
mediaPlayer.trailingAnchor.constraint(equalTo: view.trailingAnchor),
mediaPlayer.topAnchor.constraint(equalTo: view.topAnchor),
mediaPlayer.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
mediaPlayer.play()
UIApplication.shared.isIdleTimerDisabled = true
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
mediaPlayer.stop()
UIApplication.shared.isIdleTimerDisabled = false
}
}
so I am creating an app that includes an audio player that is accessed through a button called from the main page once the user has logged into their account. All of the pages of the app have been created using Storyboards but the audio player is done programmatically with SwiftUI. The app runs fine until you press the button to access the audio player. Then I get "Thread 1: Fatal error: init(coder:) has not been implemented" on the AudioPlayerViewController. So I searched for a solution online and as per the suggestions here, on stackOverflow, removed this:
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
And put this, instead, as suggested:
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
But then I get this error:
"Property 'self.album' not initialized at super.init call"
so I add: " self.album = album" - as you see below:
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.album = album
}
But then I get the error: "Assigning a property to itself"
And now I'm tired. Send help.
Let me know if I should share a link to my code or whatever else, I haven't asked a question here before.
I guess here's the whole code anyway:
import UIKit
final class AudioPlayerViewController: UIViewController {
var album: Album
private lazy var mediaPlayer: MediaPlayer = {
let v = MediaPlayer(album: album)
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
init(album: Album) {
self.album = album
super.init(nibName: nil, bundle: nil)
}
// required init?(coder: NSCoder) {
// fatalError("init(coder:) has not been implemented")
// }
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.album = album
}
// override func awakeFromNib() {
// super.awakeFromNib()
// //custom logic goes here
// }
//
override func viewDidLoad() {
super.viewDidLoad()
setupView()
}
private func setupView() {
addBlurredView()
view.addSubview(mediaPlayer)
setupConstraints()
}
private func addBlurredView() {
if !UIAccessibility.isReduceTransparencyEnabled {
self.view.backgroundColor = UIColor.clear
let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.dark)
let blueEffectView = UIVisualEffectView(effect: blurEffect)
blueEffectView.frame = self.view.bounds
blueEffectView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
view.addSubview(blueEffectView)
} else {
view.backgroundColor = UIColor.black
}
}
private func setupConstraints() {
NSLayoutConstraint.activate([
mediaPlayer.leadingAnchor.constraint(equalTo: view.leadingAnchor),
mediaPlayer.trailingAnchor.constraint(equalTo: view.trailingAnchor),
mediaPlayer.topAnchor.constraint(equalTo: view.topAnchor),
mediaPlayer.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
mediaPlayer.play()
UIApplication.shared.isIdleTimerDisabled = true
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
mediaPlayer.stop()
UIApplication.shared.isIdleTimerDisabled = false
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
欢迎来到SO!这个答案不会解决你所有的问题,但它解决了你的问题。一旦克服了这个障碍,您应该随时提出(打开/提交)其他问题。
当编译器抱怨:
“属性‘self.album’未在 super.init 调用时初始化”
时,你说你添加了:
但这并没有解决编译器刚刚抱怨的问题:
属性
self.album
在 super.init 调用时仍未初始化。您只是将其添加到了错误的位置。
调用super之前初始化self.album:
以下来自Apple的在线文档
AppleDoc
Welcome to SO! This answer will not solve all your problems, but it addresses your question. You should feel free to ask (open/submit) additional questions once you get over this speed bump.
When the compiler complained that:
"Property 'self.album' not initialized at super.init call"
You say you added:
But that doesn't address what the compiler just complained about:
The property
self.album
is still not initialized at super.init call.You just added it in the wrong place is all.
Initialize self.album before calling super:
The following is from Apple's online documentation
AppleDoc