以前可能已经问过这个问题了,但我再问一次。因为我无法找到解决问题的方法。我有一个相当简单的类,我得到这个错误。请看下面的代码。
import UIKit
import QuartzCore
protocol AddItemViewControllerDelegate {
func addItemViewControllerDidCancel(controller: InsertItemViewController)
func addItemViewController(controller: InsertItemViewController, didFinishAddingItem item: ChecklistItem)
func addItemViewController(controller: InsertItemViewController, didFinishEditingItem item: ChecklistItem)
}
class InsertItemViewController: UITableViewController, UITextFieldDelegate, UITextViewDelegate {
@IBOutlet var notesField: UITextView!
var notes: String = ""
var shouldRemind: Bool = false
var dueDate: NSDate = NSDate()
var delegate: AddItemViewControllerDelegate
var itemToEdit: ChecklistItem {
get {
return self.itemToEdit
}
set(newItem) {
if itemToEdit != newItem {
self.itemToEdit = newItem
notes = itemToEdit.notes
shouldRemind = itemToEdit.shouldRemind
dueDate = itemToEdit.dueDate
}
}
}
@IBOutlet var dueDateLabel: UILabel!
var activityViewController: UIActivityViewController!
convenience required init(coder aDecoder: NSCoder) {
self.init(coder: aDecoder)
notes = ""
shouldRemind = true
dueDate = NSDate()
}
@IBAction func cancel() {
self.delegate.addItemViewControllerDidCancel(self)
}
@IBAction func done() {
if self.itemToEdit == "" {
let item: ChecklistItem = ChecklistItem()
item.notes = self.notesField.text!
item.shouldRemind = true
item.dueDate = dueDate
self.delegate.addItemViewController(self, didFinishAddingItem: item)
}
else {
self.itemToEdit.notes = self.notesField.text!
self.itemToEdit.shouldRemind = true
self.itemToEdit.dueDate = dueDate
self.delegate.addItemViewController(self, didFinishEditingItem: self.itemToEdit)
}
}
override func viewDidLoad() {
super.viewDidLoad()
///wecreate
let doneButton: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: #selector(InsertItemViewController.done))
self.navigationItem.rightBarButtonItems = [doneButton]
if self.itemToEdit.itemId != 0 {
self.title = "Edit Note"
}
else {
self.title = "Add Note"
}
self.notesField.text = notes
self.notesField.delegate = self
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
}
override func scrollViewWillBeginDragging(scrollView: UIScrollView) {
self.notesField.resignFirstResponder()
}
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.section == 0 {
cell.backgroundColor = TableCellBackgroundColor
}
}
override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
if indexPath.row == 0 {
return indexPath
} else {
return nil;
}
}
func textViewShouldBeginEditing(textView: UITextView) -> Bool {
return true
}
func imageFromLayer(layer: CALayer) -> UIImage {
layer.renderInContext(UIGraphicsGetCurrentContext()!)
let outputImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return outputImage
}
}
我无法确定此错误背后的原因。任何人都可以看看代码,告诉我我错了什么?我是Swift的新手,所以请原谅我的忽视。
答案 0 :(得分:2)
您的问题是,类中的所有属性都需要在init
或之前初始化,但需要初始化,您可以修复它,将您的委托声明为Optional
正如@Sultan在他的评论中所说,你应该总是使用weak
引用来实现你的代理,以避免任何类型的保留周期,因为你需要在协议中指定它总是由类实现,就像这样:
protocol AddItemViewControllerDelegate: class {
func addItemViewControllerDidCancel(controller: InsertItemViewController)
func addItemViewController(controller: InsertItemViewController, didFinishAddingItem item: ChecklistItem)
func addItemViewController(controller: InsertItemViewController, didFinishEditingItem item: ChecklistItem)
}
然后你可以在你的类中声明为weak
引用,如下所示:
weak var delegate: AddItemViewControllerDelegate?
我希望这对你有所帮助。
答案 1 :(得分:0)
由于此行,您收到此错误。
var delegate: AddItemViewControllerDelegate
用此替换该行。
var delegate: AddItemViewControllerDelegate!
但是如果你没有初始化委托那么它就会崩溃。因此,为避免崩溃,请使用以下内容。
var delegate: AddItemViewControllerDelegate?