- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
protocol FooType {
var num:Int { get set }
}
class Foo: FooType {
var num: Int = 0 {
didSet {
print("Did set num")
}
}
}
class Bar {
var foo: FooType = Foo() {
didSet {
print("Did set Foo")
}
}
func changeNum(num:Int) {
foo.num = num
}
}
let bar = Bar()
bar.changeNum(5)
Did set num
Did set Foo
在此示例中,为 foo 设置属性会导致调用 Bar 的 foo 的 didSet。
我希望只有 Foo 的 num didSet 被调用。
如果我移除 Bar 的 foo 属性对 FooType 的协议(protocol)约束,它会按照我的预期运行。
protocol FooType {
var num:Int { get set }
}
class Foo: FooType {
var num: Int = 0 {
didSet {
print("Did set num")
}
}
}
class Bar {
var foo = Foo() {
didSet {
print("Did set Foo")
}
}
func changeNum(num:Int) {
foo.num = num
}
}
let bar = Bar()
bar.changeNum(5)
Did set num
如果我保持与 FooType 的一致性,但添加一个类约束 (FooType: class),它也会按预期运行。
protocol FooType: class {
var num:Int { get set }
}
class Foo: FooType {
var num: Int = 0 {
didSet {
print("Did set num")
}
}
}
class Bar {
var foo: FooType = Foo() {
didSet {
print("Did set Foo")
}
}
func changeNum(num:Int) {
foo.num = num
}
}
let bar = Bar()
bar.changeNum(5)
Did set num
如果我完全删除协议(protocol)并使 Foo 成为一个结构而不是一个类,我们将返回调用两个 setter 的情况。
struct Foo {
var num: Int = 0 {
didSet {
print("Did set num")
}
}
}
class Bar {
var foo = Foo() {
didSet {
print("Did set Foo")
}
}
func changeNum(num:Int) {
foo.num = num
}
}
let bar = Bar()
bar.changeNum(5)
Did set num
Did set Foo
在将 Foo 更改为结构的情况下,我可以理解为什么会这样……它在 swift 文档中提到了它,因为结构是一种值类型,它会生成一个副本,等等(虽然我没想到一开始)。
但其他情况我没有得到...
最佳答案
你可以通过让 FooType 符合 AnyObject 来解决这个问题
protocol FooType: AnyObject {
var num:Int { get set }
}
关于swift - 为什么调用此属性的 didSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36568261/
我正在对此进行测试,如果您更改 didSet 中的值,您将不会再次调用 didSet。 var x: Int = 0 { didSet { if x == 9 { x = 10
我在玩 SwiftUI,我有一个看起来像这样的类: class Foo: ObservableObject { @Published var foo: Int! { // Implicit u
这是我在 Swift 中的第一个项目,所以请耐心等待。 punkteLimit 应初始化为值 30。每次更改 slider 值时,都应更新变量的值以及标签。 var punkteLimit: Int
我有这个代码: class Child { var data : String?; func updateData (s: String) { data = s; } } class
我有这个代码: class Bar: UIViewController { lazy var foo = Foo() foo.bar { didSet { barLabel
假设我有一个具有 3 个属性的 ExerciseSet 类:id、name、isEnabled。 我有一个这个类的对象数组: var exerciseSets: [ExerciseSet] = []
我有一个变量,当你设置和改变时我想要一个监听器,而 didSet 似乎是完美的候选者。但是,在我对它的初始测试中,第一个附加触发了 did 集,并且(在我的示例中)JS 加载到我的 webview 中
给定以下类(class), class Spaceship { var position: CGPoint! { didSet { node.posit
xcode 版本:7.3.1 大家好 我正在关注最新的 Stanford Uni iOS 类(class)。在正在创建的其中一个应用程序中,我们创建了一个具有许多属性的结构。 因为它是一个结构,所以我
我正在尝试将数据从我的 cellClass 传递到 viewController。我可以轻松地将标签文本从 cellClass 传递到 vc,但无法在 vc 中设置变量。 这是我的模型类: class
假设我有一个单例经理 class Manager { static let sharedInstance = Manager() var text: String {
假设我有一个数组: var intArray: [Int] = [1,2,3,4,5] { didSet{ //print index of value that was mo
protocol FooType { var num:Int { get set } } class Foo: FooType { var num: Int = 0 {
我们在 didSet 中使用循环时遇到了这种奇怪的行为。我们的想法是我们有一个具有树结构的数据类型,并且在每个元素中我们想要存储该项目所在的级别。因此,在 level 属性的 didSet 中,我们还
我想覆盖属性观察器并调用“super.didSet”。这可能吗? class Foo { var name: String = "" { didSet { print("name has be
我需要在不更改属性值的情况下执行 didSet 中的代码。 最近从Objective-C过来的,好像没有setMyProperty()。 所以我尝试了: self.myProperty = self.
我想知道“didSet”是否适用于数组中的元组。 我正在编写类似下面的代码,我想观察数组中元组的值。可以做那种事吗? var array :[(foo: Int, bar: Int)] = []]{
在此代码中,当文本更改时,将调用 titleEditingChanged(如预期的那样)。但是当它执行行时 investment?.title = sender.text! 它调用 Investmen
我有一个包含图像的 Collection View 。每个 Collection View 项目都有与它们关联的图像和其他数据,例如“grand_total”、“product_id”等。现在每个 C
当我在 didSet 中创建一个递归时,实际上我发现我可以只放一个 return 并且程序从 didSet 退出。但是我没有找到任何地方(我搜索了很长时间)我可以放一个返回词来退出 didSet。那么
我是一名优秀的程序员,十分优秀!