Seth Rodgers Seth Rodgers - 5 months ago 11
Swift Question

Why am I getting nil for this string value?

Basically I'm trying to make a simple note app, and here I'm just trying to display the title and content.

Project (note) file

import Foundation

class Project
{
var title = " "
var content = " "
var after = " "
}


Note detail view controller file

class NoteDetailViewController: UIViewController
{
var project: Project!

@IBOutlet weak var titleTextField: UITextField!
@IBOutlet weak var contentTextField: UITextView!
@IBOutlet weak var afterTextField: UITextView!

override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
titleTextField.text = project.title
contentTextField.text = project.content
afterTextField.text = project.after

contentTextField.sizeToFit()
afterTextField.sizeToFit()
}

override func viewWillDisappear(animated: Bool)
{
super.viewWillDisappear(animated)

project.title = titleTextField.text!
project.content = contentTextField.text!
}

}


When I reference any of the project. files, I get nil. I have no idea why I'm getting nil from a string?

Answer

You need to initialize the class "Project" correctly.

let project = Project()