Swift / Objective C: How to get the value of an object by string name

class A {
  var x = 1

var a = A()

How to get a variable "x" from object "a" using string name ( a["x"] )?

This will work if the class inherits from NSObject, where you can use valueForKey: to get at the properties.

import Foundation

class A: NSObject {
  var x = 1

let a = A()
let aval = a.valueForKey("x")

Note that aval is an AnyObject? here since there's no type information. You'll need to cast it or test what it is yourself.