swift Cannot find 'self' in scope报错

1,032 阅读1分钟

没见过这种错误 原代码:

/// 缓存的行高
    var rowHeight: CGFloat = {
        print("计算行高 \(self.status.text)")
        // 1. cell
        let cell = StatusCell(style: .default, reuseIdentifier: StatusCellNormalId)
        
        // 2. 记录高度
        return cell.rowHeight(vm: self)
    }()

原来是没有添加lazy

    /// 缓存的行高
    lazy var rowHeight: CGFloat = {
        print("计算行高 \(self.status.text ?? "")")
        // 1. cell
        let cell = StatusCell(style: .default, reuseIdentifier: StatusCellNormalId)
        // 2. 记录高度
        return cell.rowHeight(vm: self)
    }()

瞬间变好了