In lecture 6 Stanford iOS9 2016 lecture series, a file called VCL.swift was added to the program to demonstrate view controller lifecycle.
I have added the file, but for line "print("\(logPrefix)Emotions\(instance)" + msg)
and line "print("\(logPrefix)Face \(instance)" + msg)"
, xcode is displaying an error, telling me that "use of unresolved identifier 'instance' ".
Does anyone has any idea how to fix it?
Below is the VCL.swift file from lecture 6
import UIKit
private var faceMVCinstanceCount = 0
func getFaceMVCinstanceCount() -> Int { faceMVCinstanceCount += 1; return faceMVCinstanceCount }
private var emotionsMVCinstanceCount = 0
func getEmotionsMVCinstanceCount() -> Int { emotionsMVCinstanceCount += 1; return emotionsMVCinstanceCount }
var lastLog = NSDate()
var logPrefix = ""
func bumpLogDepth() {
if lastLog.timeIntervalSinceNow < -1.0 {
logPrefix += "__"
lastLog = NSDate()
}
}
// we haven't covered extensions as yet
// but it's basically a way to add methods to a given class
extension FaceViewController
{
func logVCL(msg: String) {
bumpLogDepth()
print("\(logPrefix)Face \(instance) " + msg) //error: use of unresolved identifier 'instance'
}
override func awakeFromNib() {
logVCL("awakeFromNib()")
}
override func viewDidLoad() {
super.viewDidLoad()
logVCL("viewDidLoad()")
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
logVCL("viewWillAppear(animated = \(animated))")
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
logVCL("viewDidAppear(animated = \(animated))")
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
logVCL("viewWillDisappear(animated = \(animated))")
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
logVCL("viewDidDisappear(animated = \(animated))")
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
logVCL("viewWillLayoutSubviews() bounds.size = \(view.bounds.size)")
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
logVCL("viewDidLayoutSubviews() bounds.size = \(view.bounds.size)")
}
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
logVCL("viewWillTransitionToSize")
coordinator.animateAlongsideTransition({ (context: UIViewControllerTransitionCoordinatorContext!) -> Void in
self.logVCL("animatingAlongsideTransition")
}, completion: { context -> Void in
self.logVCL("doneAnimatingAlongsideTransition")
})
}
}
extension EmotionsViewController
{
func logVCL(msg: String) {
bumpLogDepth()
print("\(logPrefix)Emotions \(instance) " + msg) //error: use of unresolved identifier 'instance'
}
override func awakeFromNib() {
logVCL("awakeFromNib()")
}
override func viewDidLoad() {
super.viewDidLoad()
logVCL("viewDidLoad()")
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
logVCL("viewWillAppear(animated = \(animated))")
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
logVCL("viewDidAppear(animated = \(animated))")
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
logVCL("viewWillDisappear(animated = \(animated))")
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
logVCL("viewDidDisappear(animated = \(animated))")
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
logVCL("viewWillLayoutSubviews() bounds.size = \(view.bounds.size)")
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
logVCL("viewDidLayoutSubviews() bounds.size = \(view.bounds.size)")
}
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
logVCL("viewWillTransitionToSize")
coordinator.animateAlongsideTransition({ (context: UIViewControllerTransitionCoordinatorContext!) -> Void in
self.logVCL("animatingAlongsideTransition")
}, completion: { context -> Void in
self.logVCL("doneAnimatingAlongsideTransition")
})
}
}
The error: "use of unresolved identifier 'instance'" means that you are trying to use a variable, that hasn't been declared.
In your case you are lacking a line let instance = getEmotionsMVCinstanceCount()
in the EmotionsViewController
(EmotionsViewController.swift) and/or let instance = getFaceMVCinstanceCount()
in the FaceViewController
(FacialExpression.swift).
Just take a look on a full source available here: Lecture 6: Multiple MVCs