gpt4 book ai didi

ios - 从存储在变量中的类型获取静态属性的内容

转载 作者:行者123 更新时间:2023-11-28 09:09:32 27 4
gpt4 key购买 nike

下面的代码非常人为设计,但最重要的是我有一个包含类型的变量(例如 let someType: AnyObject = SomeClass.self),我想获取静态的值属于存储在该变量中的类型的属性。这在 Swift 中可能吗?

class SomeClass {
var someDict: Dictionary<String, AnyObject> = {
"aKey": SomeOtherClass.self
"anotherKey": SomeOtherOtherClass.self
}

func someFunction() {
let someType: AnyObject? = someDict["aKey"]

// This line is incorrect, but how would get the static value I am after?
let aString: String = someType.someString
println(aString)
}
}

class SomeOtherClass {
static var someString: String = "Hello World"
}

class SomeOtherOtherClass {
static var someString: String = "Foo Bar"
}

最佳答案

答案是将 AnyObject 引用更改为 SomeOtherBaseClass.Type,其中 SomeOtherBaseClass 是 SomeOtherClassSomeOtherOtherClass< 的父类.

class SomeClass {
var someDict: Dictionary<String, SomeOtherBaseClass.Type> = {
"aKey": SomeOtherClass.self
"anotherKey": SomeOtherOtherClass.self
}

func someFunction() {
let someType: SomeOtherBaseClass.Type? = someDict["aKey"]

// This line is incorrect, but how would get the static value I am after?
let aString: String = someType!.someString
println(aString)
}
}

class SomeOtherClass {
static var someString: String = "Hello World"
}

class SomeOtherOtherClass {
static var someString: String = "Foo Bar"
}

感谢@lukya 将我推向正确的方向。

关于ios - 从存储在变量中的类型获取静态属性的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29708021/

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