我想要将一个可编码的 struct 转换成键值对,这样我就可以把它写到CSV中的一行中,这样我就可以用EXCEL或Apple数字来表示它.我需要在CSV的一些单元格,以参考为基础的计算.
例如,假设我有A1作为收入,A2作为支出,我想让A3作为净yield 或"=A1-A2"
struct FinanceSummary {
var expense: Decimal
var revenue: Decimal
var lineReference: Int { #line + 2 } // + 2 for the 2 lines below before key pairs
func keyPairs(row: Int) -> KeyValuePairs<String, String> {
[
"expense": "\(expense)",
"revenue": "\(revenue)",
"income": "=A\(#line - lineReference - 1) - A\(#line - lineReference - 2)"
]
}
}
然而,在我过go 可以使用#line
来获取行号的地方,当我try 时,我得到了一个错误:#line directive was renamed to #sourceLocation
,当我使用#sourceLocation
时,我得到了另一个错误:Expected '(' in #sourceLocation directive
,因为编译器希望我用文件和行号来初始化源位置.但是,我不想告诉编译器,我在什么文件和行上.我想让编译器告诉我.