你能从来电者那里得到__FUNCTION__吗?

时间:2015-12-22 15:25:19

标签: swift macros fatal-error

示例:

@noreturn func setOnlyPropertyGetterError(__function__: String) {
   fatalError("\(__function__) is set-only")
}

var property: Property {
   get {setOnlyPropertyGetterError(__FUNCTION__)}
   set {//useful work}
}

我们可以避免通过__FUNCTION__吗?

1 个答案:

答案 0 :(得分:1)

我认为这是你想要实现的目标:

@noreturn func writeOnlyProperty(propertyName: String = __FUNCTION__) {
    fatalError("Property \(propertyName) is write-only")
}

class Foo {
    var blackHole: Int {
        get { writeOnlyProperty() }
        set { print("Consuming value \(newValue)") }
    }
}

let foo = Foo()
foo.blackHole = 1       // Prints "Consuming value 1"
let bar = foo.blackHole // Produces fatal error "Property blackHole is write-only"