Reputation:
Probably this question have been asked previously, yet i am asking again. Because i couldn't find a solution for my problem. I have a fairly simple class on which i am getting this error. Please take a look at the bellow code.
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
}
}
I am not able to identify the reason behind this error. Can anyone please take a look into the code and tell me what i am going wrong ? I am new to Swift, so please pardon my ignorence.
Upvotes: 1
Views: 287
Reputation: 23449
Your problem is that all the properties inside a class need to be initialized in the init
or before, but need to be initialized, you can fix it declaring your delegate as Optional
As @Sultan said in his comment you should always implement your delegates using weak
references to avoid any kind of retain-cycles, for do that you need to specify in your protocol that it always be implemented by classes, like in this way:
protocol AddItemViewControllerDelegate: class {
func addItemViewControllerDidCancel(controller: InsertItemViewController)
func addItemViewController(controller: InsertItemViewController, didFinishAddingItem item: ChecklistItem)
func addItemViewController(controller: InsertItemViewController, didFinishEditingItem item: ChecklistItem)
}
And then you can do declare as a weak
reference inside your class like this:
weak var delegate: AddItemViewControllerDelegate?
I hope this help you.
Upvotes: 2
Reputation: 2330
You are getting this error because of this line.
var delegate: AddItemViewControllerDelegate
Replace that line with this.
var delegate: AddItemViewControllerDelegate!
But if you are not initializing the delegate then it will crash. So to avoid crash, use the following.
var delegate: AddItemViewControllerDelegate?
Upvotes: 0