斯坦福iOS9 2016讲座6 VCL.swift文件,错误行“print(”\(logPrefix)Face \(instance)“+ msg)”

问题描述 投票:1回答:2

在第6讲斯坦福iOS9 2016系列讲座中,一个名为VCL.swift的文件被添加到程序中以演示视图控制器生命周期。

我添加了文件,但对于行"print("\(logPrefix)Emotions\(instance)" + msg)和行“print("\(logPrefix)Face \(instance)" + msg)",xcode显示错误,告诉我”使用未解析的标识符'实例'“。

有谁知道如何解决它?

以下是第6讲的VCL.swift文件

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")
        })
    }
}
ios iphone swift ios9 cs193p
2个回答
2
投票

错误:“使用未解析的标识符'实例'”意味着您正在尝试使用尚未声明的变量。

在你的情况下,你在let instance = getEmotionsMVCinstanceCount()(EmotionsViewController.swift)和/或EmotionsViewController(FacialExpression.swift)中缺少一行let instance = getFaceMVCinstanceCount()

只需看看这里提供的完整资源:FaceViewController


2
投票

该错误意味着您正在尝试打印Lecture 6: Multiple MVCs,但您还没有在任何地方的代码中定义它。两次使用instance标识符检查您的代码是否有对象或变量。

希望这会有所帮助:)

© www.soinside.com 2019 - 2024. All rights reserved.