gpt4 book ai didi

swift - 你能从调用者那里得到 __FUNCTION__ 吗?

转载 作者:行者123 更新时间:2023-11-30 10:08:44 26 4
gpt4 key购买 nike

示例:

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

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

我们可以避免必须传递__FUNCTION__吗?

最佳答案

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

@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"

关于swift - 你能从调用者那里得到 __FUNCTION__ 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34419129/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com