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

标签 swift macros fatal-error

示例:

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

相关文章:

c - 使用 -D 引用宏预处理器

php - PHP 中是否可以防止 "Fatal error: Call to undefined function"?

SwiftUI - 来自 Coordinator 的 ObservableObject 更新

c - 如何在C宏中区分常量字符串和char *

ios - 获取一周的工作日和日期

macros - 在 SAS 中自动扫描和搜索表/对象名称

php - PHP 中 PDO::MYSQL_ATTR_MAX_BUFFER_SIZE 发生 fatal error

php - 类包含 1 个抽象方法,因此必须声明为抽象方法或实现其余方法

ios - 如何在 Swift 中显示动态高度的 3*3 CollectionView 单元格

ios - 侧边菜单和桌面 View 自定义