ios - 结构默认初始化程序不触发 didSet

标签 ios swift xcode struct

xcode 版本:7.3.1

大家好

我正在关注最新的 Stanford Uni iOS 类(class)。在正在创建的其中一个应用程序中,我们创建了一个具有许多属性的结构。

因为它是一个结构,所以我得到了一个免费的初始化器,我在我的 Controller 中调用它。然后,我将 struct instance 设为计算属性,以便调用 didSet。

这是结构

struct FacialExpression
{
var eyes: Eyes = Eyes.Open
var eyeBrows: EyeBrows = EyeBrows.Normal
var mouth: Mouth = Mouth.Smile

enum Eyes: Int {
    case Open
    case Closed
    case Squinting
}

enum EyeBrows: Int {
    case Relaxed
    case Normal
    case Furrowed

    func moreRelaxedBrow() -> EyeBrows {
        return EyeBrows(rawValue: rawValue - 1) ?? .Relaxed
    }
    func moreFurrowedBrow() -> EyeBrows {
        return EyeBrows(rawValue: rawValue + 1 ) ?? .Furrowed
    }
}

enum Mouth: Int {
    case Frown
    case Smirk
    case Neutral
    case Grin
    case Smile

}
}

这是创建结构实例的 Controller

class FaceController: UIViewController {

var expression = FacialExpression(eyes: .Open, eyeBrows: .Normal, mouth: .Smile) {

    didSet {
        updateUI()
    }

}
}

此 Controller 是应用程序中的默认且唯一的 Controller ,并且会调用 init 行。

didSet() 永远不会被调用。

谁能解释一下为什么?

最佳答案

在初始化期间不会调用属性观察器。

看看this问题。

关于ios - 结构默认初始化程序不触发 didSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38612142/

相关文章:

c++ - 链接器命令失败,退出代码为 1 C++

ios - 使用 reuseIdentifier 调用 super.tableView(tableView : UITableView, cellForRowAtIndexPath...

c - 为什么 'wait' 函数总是返回 -1?

ios - 如何在 View Controller 中水平和垂直居中 MKMapView?

ios - UITableview 单元格未在 reloaddata 上更新

ios - 编辑 UITableViewCell 从 CoreData 提取数据

ios - 控制台输出中的 lldb 错误,无法获得正确的结果

ios - FHSTwitterEngine 无法发布带有图像的推文

iphone - 如何在iOS中将base64编码的文档转换为多页UIImageView?

ios - 获取parentViewController实例