我想知道是否可以動態獲取Swift類型。例如,假設我們有以下的嵌套結構:是否有可能從字符串中獲取Swift類型?
struct Constants {
struct BlockA {
static let kFirstConstantA = "firstConstantA"
static let kSecondConstantA = "secondConstantA"
}
struct BlockB {
static let kFirstConstantB = "firstConstantB"
static let kSecondConstantB = "secondConstantB"
}
struct BlockC {
static let kFirstConstantC = "firstConstantBC"
static let kSecondConstantC = "secondConstantC"
}
}
它可以從一個變量從kSeconConstantC獲得價值)。像:
let variableString = "BlockC"
let constantValue = Constants.variableString.kSecondConstantC
東西類似NSClassFromString
,也許?
值不能用作標識符,根據我的想法我認爲這是不可能的 –
你可以使用像if(variableString ==「BlockC」)然後做點什麼 –
謝謝你的讚揚,但背後的想法是減少碼。我更喜歡(如果存在)解決方案,而不是爲每個案例寫幾個if-else。 – RFG