willSet didSet语法?喜欢它是真的吗? [关闭]

问题描述 投票:-6回答:1

在类和结构的代码中,willSet和didSet观察者是一个东西吗?它们是真正面向对象的编程。很多Sulo代码所以只想知道

swift
1个回答
0
投票

如果我正确理解你的问题,是的,Swift有内置的setSet和didSet属性观察:

struct SomeStruct {
   var someProperty: Int {
      willSet {
         print("Hello, ")
      }
      didSet {
         print("World!")
      }
   }
}

class SomeClass {
   var someProperty: Int {
      willSet {
         print("Hello, ")
      }
      didSet {
         print("World!")
      }
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.