1

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")
        })
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Thor
  • 9,638
  • 15
  • 62
  • 137

2 Answers2

2

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

Mikolaj
  • 802
  • 10
  • 18
  • thank you very much for helping out. but xcode is printing out what i believe is address in memory instead of 1, 2, 3 etc. Something like -------------- Face 1 awakeFromNib() Emotions 1 awakeFromNib() – Thor Jun 01 '16 at 07:35
  • @TonyStark - Did you change the line: `print("\(logPrefix)Face \(instance) " + msg)` in logVCL functions? Try do download the full source code from the link above and check it (it works perfectly fine for me). If this would work, you can check what is different in your solution. – Mikolaj Jun 01 '16 at 08:13
  • And yes - it's the memory address. It looks like added `\(self)` to the print statement. – Mikolaj Jun 01 '16 at 08:36
  • Thanks again for your help! yes you were right. I was messing around with the print statement because I wasn't getting the right output, but now everything is working fine. Thanks to you! I seriously can't thank you enough, would have took me forever to solve the problem. – Thor Jun 01 '16 at 13:29
2

That error means you are trying to printing instance but you haven't define it in code anywhere. Twice check your code that you have object or variable is there with instance identifier.

Hope this will help :)

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75