有没有办法在Swift中列出类的所有变量?
例如:
class foo {
var a:Int? = 1
var b:String? = "John"
}
Run Code Online (Sandbox Code Playgroud)
我想这样列出: [a:1, b:"John"]
我想创建一个Objective-C基类,它在运行时对所有属性(不同类型)执行操作.由于不会总是知道属性的名称和类型,我该怎么做这样的事情呢?
@implementation SomeBaseClass
- (NSString *)checkAllProperties
{
for (property in properties) {
// Perform a check on the property
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:这在自定义- (NSString *)description:覆盖中特别有用.