gpt4 book ai didi

ios - 无法弄清楚编码器 aDecoder : NSCoder

转载 作者:可可西里 更新时间:2023-11-01 01:39:47 24 4
gpt4 key购买 nike

我正在制作一个用于存档书籍的应用程序,我正准备连接一个按钮来保存数据,但我无法通过初始化。

这是 Xcode 给我的错误:

Property 'self.bookStore' not initialised at super.init call.

这是我的代码:

import UIKit

class AddBook: UIViewController {

@IBOutlet weak var bookAuthor: UITextField!
@IBOutlet weak var bookTitle: UITextField!

let bookStore: BookStore

init(bookStore: BookStore) {
self.bookStore = bookStore
super.init(nibName: nil, bundle: nil)
}

required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder) // Error: Property 'self.bookStore' not initialised at super.init call.
}


@IBAction func saveNewBook(sender: AnyObject) {
let author = self.bookAuthor.text!
let title = self.bookTitle.text!

bookStore.addBook(author, title: title)
println("\(bookStore)")
}
}


import UIKit

class BookStore: NSObject {

var allBooks: [Book] = []

func addBook(author: String, title: String) -> Book {
let newBook = Book(author: author, title: title)

allBooks.append(newBook)

return newBook
}
}

最佳答案

您的自定义初始化程序无法初始化不可变属性。如果您希望它是不可变的,则无需创建自定义初始化程序,只需初始化所需或指定的初始化程序之一即可。

像这样,

class AddBook: UIViewController {

@IBOutlet weak var bookAuthor: UITextField!
@IBOutlet weak var bookTitle: UITextField!

let bookStore: BookStore

required init(coder aDecoder: NSCoder) {
fatalError("Cannot be instantiated from storyboard")
}

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
bookStore = BookStore()
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}


@IBAction func saveNewBook(sender: AnyObject) {
let author = self.bookAuthor.text!
let title = self.bookTitle.text!

bookStore.addBook(author, title: title)
}
}

或者这个,

class AddBook: UIViewController {

@IBOutlet weak var bookAuthor: UITextField!
@IBOutlet weak var bookTitle: UITextField!

let bookStore: BookStore

required init(coder aDecoder: NSCoder) {
bookStore = BookStore()
super.init(coder: aDecoder)
}

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
fatalError("Cannot be instantiated from code")

}


@IBAction func saveNewBook(sender: AnyObject) {
let author = self.bookAuthor.text!
let title = self.bookTitle.text!

bookStore.addBook(author, title: title)
}
}

但是,在你的情况下,你希望它是不可变的,因为你正在使用 let bookStore: BookStore 但如果你没有使用 Storyboard,你可以简单地从所需的初始化程序中抛出致命异常并使用你的像这样拥有初始化程序,

class AddBook: UIViewController {

@IBOutlet weak var bookAuthor: UITextField!
@IBOutlet weak var bookTitle: UITextField!

let bookStore: BookStore

init(bookStore: BookStore) {
self.bookStore = bookStore
super.init(nibName: nil, bundle: nil)
}

required init(coder aDecoder: NSCoder) {
fatalError("cannot be initialized from storyboard")
}

@IBAction func saveNewBook(sender: AnyObject) {
let author = self.bookAuthor.text!
let title = self.bookTitle.text!

bookStore.addBook(author, title: title)
}
}


let book = AddBook(bookStore: BookStore())

现在,您需要具有所需的初始化程序,您必须使属性可变,以便您可以像这样从自定义初始化程序中设置它,

class AddBook: UIViewController {

@IBOutlet weak var bookAuthor: UITextField!
@IBOutlet weak var bookTitle: UITextField!

var bookStore: BookStore!

init(bookStore: BookStore) {
self.bookStore = bookStore
super.init(nibName: nil, bundle: nil)
}

required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

@IBAction func saveNewBook(sender: AnyObject) {
let author = self.bookAuthor.text!
let title = self.bookTitle.text!

bookStore.addBook(author, title: title)
}
}


let book = ... // initialized from storyboard or using performSegueWithIdentifier:

book.bookStore = BookStore()

这里的主要理念是,没有两个初始化器可以初始化不可变属性。只有一个可以做到,所以要么使用 required initializer 来实例化它,要么使 required initializer 不可用,或者使属性可变,这样你就可以有两个初始化器,并且都可以初始化它。

关于ios - 无法弄清楚编码器 aDecoder : NSCoder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30988988/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com