private(set) internal var config: Int = 0
可选类型
没有初始值就是可选
初始化时一定有值,是 隐性可选
初始化时不一定有值,是 显性可选
Repeat animation forever in Swift (HUGE_VALF)?
Set the repeatCount to Float.infinity. This compiles and works.
In all probability, HUGE_VALF was a legacy value in any case.
Still, it's a bit of a surprise that these numeric constant names are not seen by Swift. I did try importing
Defer
Allow didSet to be called during initialization in swift
class Classy {
var foo: Int! { didSet { doStuff() } }
init( foo: Int ) {
// closure invokes didSet
({ self.foo = foo })()
}
}
参考 http://stackoverflow.com/a/29501998/3548428
class SomeClass {
var someProperty: AnyObject! {
didSet {
//do some Stuff
}
}
init(someProperty: AnyObject) {
setSomeProperty(someProperty)
}
func setSomeProperty(newValue:AnyObject) {
self.someProperty = newValue
}
}
参考 http://stackoverflow.com/a/25231068/3548428
public class MyNewType : NSObject {
public var myRequiredField:Int
public var myOptionalField:Float? {
willSet {
if let newValue = newValue {
print("I'm going to change to \(newValue)")
}
}
didSet {
if let myOptionalField = self.myOptionalField {
print("Now I'm \(myOptionalField)")
}
}
}
override public init() {
self.myRequiredField = 1
super.init()
// Non-defered
self.myOptionalField = 6.28
// Defered
defer {
self.myOptionalField = 3.14
}
}
}