Search code examples
iosswiftviewdidload

SWIFT - Property not seen outside viewdidload


I try to pass a string from a VC1 to VC2, but the value of tex passed into property detailItem as loaded only into viewdidload, and not in the function

viewcontroller 1:

@IBOutlet var tex: UITextField!
// Prepare fore segue pass the value of txt

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  if segue.identifier? == "Todo" {
    let navVC = segue.destinationViewController as UINavigationController
    let itemVC: ChecklistViewController = navVC.topViewController as ChecklistViewController
    itemVC.detailItem! = tex.text
  }
 }

viewcontroller 2:

var detailItem : String!

// I can see the value of tex only in the ViewDidLoad

override func viewDidLoad() {
  super.viewDidLoad()

  println("viewDidLoad \(detailItem)")
  tableView.rowHeight = 44
}

// Here and in  the function below result nil.
required init(coder aDecoder: NSCoder) {
  println("print init \(detailItem)")
  items = [ChecklistItem]()
  super.init(coder: aDecoder)
  loadChecklistItems()
}

func documentsDirectory() -> String {
  let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as [String]
  return paths[0]
}

func dataFilePath() -> String {
  println("print path\(detailItem)")
  return documentsDirectory().stringByAppendingPathComponent("Checklists.plist")
}

func saveChecklistItems() {
  let data = NSMutableData()
  let archiver = NSKeyedArchiver(forWritingWithMutableData: data)
  archiver.encodeObject(items, forKey: "ChecklistItems")
  archiver.finishEncoding()
  data.writeToFile(dataFilePath(), atomically: true)
  println("print save\(detailItem)")
}

func loadChecklistItems() {
  let path = dataFilePath()
  if NSFileManager.defaultManager().fileExistsAtPath(path) {
    if let data = NSData(contentsOfFile: path) {
      println("print load\(detailItem)")
      let unarchiver = NSKeyedUnarchiver(forReadingWithData: data)
      items = unarchiver.decodeObjectForKey("ChecklistItems") as [ChecklistItem]
      unarchiver.finishDecoding()
    }
  }
}

Console Output:

print init 
print path
print load
viewDidLoad Antwerp

Thanks Alberto


Solution

  • You can't see this property in the initializer because that code is called before itemVC.detailItem! = tex.text.

    You could move your call to loadChecklistItems() into viewDidLoad(). You could also load the items as soon as the detail item is set:

    var detailItem : String! {
        didSet {
            loadChecklistItems()
        }
    }