title: Get Property List of Swift Class
body: <p>I am trying to get the property list of a swift class. There is a similar question <a href="https://stackoverflow.com/questions/24844681/list-of-classs-properties-in-swift">here</a> and <a href="https://stackoverflow.com/questions/24219179/how-do-i-serialise-nsdictionary-from-a-class-swift-implementation">here</a>.  I have everything working except for certain types do not get returned from <code>class_copyPropertyList</code>. The ones I have tested it with so far are <code>Int?</code> and enums.  I have an example class below of what I am trying.</p>
  
  <pre><code>enum PKApiErrorCode: Int {
      case None = 0
      case InvalidSignature = 1
      case MissingRequired = 2
      case NotLoggedIn = 3
      case InvalidApiKey = 4
      case InvalidLogin = 5
      case RegisterFailed = 6
  }
  
  class ApiError: Serializable {
      let code: PKApiErrorCode?
      let message: String?
      let userMessage: String?
  
      init(error: JSONDictionary) {
          code = error["errorCode"] &gt;&gt;&gt; { (object: JSON) -&gt; PKApiErrorCode? in
              if let c = object &gt;&gt;&gt; JSONInt {
                  return PKApiErrorCode.fromRaw(c)
              }
  
              return nil
          }
  
          message = error["message"] &gt;&gt;&gt; JSONString
          userMessage = error["userMessage"] &gt;&gt;&gt; JSONString
      }
  }
  </code></pre>
  
  <p>And the Serializable class (with some help from <a href="https://gist.github.com/turowicz/e7746a9c035356f9483d" rel="nofollow noreferrer">https://gist.github.com/turowicz/e7746a9c035356f9483d</a> is</p>
  
  <pre><code>public class Serializable: NSObject, Printable {
      override public var description: String {
          return "\(self.toDictionary())"
      }
  }
  
  extension Serializable {
       public func toDictionary() -&gt; NSDictionary {
          var aClass : AnyClass? = self.dynamicType
          var propertiesCount : CUnsignedInt = 0
          let propertiesInAClass : UnsafeMutablePointer&lt;objc_property_t&gt; = class_copyPropertyList(aClass, &amp;propertiesCount)
          var propertiesDictionary : NSMutableDictionary = NSMutableDictionary()
          for var i = 0; i &lt; Int(propertiesCount); i++ {
  
              var property = propertiesInAClass[i]
              var propName = NSString(CString: property_getName(property), encoding: NSUTF8StringEncoding)
              var propType = property_getAttributes(property)
              var propValue : AnyObject! = self.valueForKey(propName);
              if propValue is Serializable {
                  propertiesDictionary.setValue((propValue as Serializable).toDictionary(), forKey: propName)
              } else if propValue is Array&lt;Serializable&gt; {
                  var subArray = Array&lt;NSDictionary&gt;()
                  for item in (propValue as Array&lt;Serializable&gt;) {
                      subArray.append(item.toDictionary())
                  }
                  propertiesDictionary.setValue(subArray, forKey: propName)
              } else if propValue is NSData {
                  propertiesDictionary.setValue((propValue as NSData).base64EncodedStringWithOptions(nil), forKey: propName)
              } else if propValue is Bool {
                  propertiesDictionary.setValue((propValue as Bool).boolValue, forKey: propName)
              } else if propValue is NSDate {
                  var date = propValue as NSDate
                  let dateFormatter = NSDateFormatter()
                  dateFormatter.dateFormat = "Z"
                  var dateString = NSString(format: "/Date(%.0f000%@)/", date.timeIntervalSince1970, dateFormatter.stringFromDate(date))
                  propertiesDictionary.setValue(dateString, forKey: propName)
  
              } else {
                  propertiesDictionary.setValue(propValue, forKey: propName)
              }
          }
  
          return propertiesDictionary
      }
  
      public func toJSON() -&gt; NSData! {
          var dictionary = self.toDictionary()
          var err: NSError?
          return NSJSONSerialization.dataWithJSONObject(dictionary, options:NSJSONWritingOptions(0), error: &amp;err)
      }
  
      public func toJSONString() -&gt; NSString! {
          return NSString(data: self.toJSON(), encoding: NSUTF8StringEncoding)
      }
  }
  </code></pre>
  
  <p>The String seem to only appear if the Optionals are valid values and the <code>code</code> never appears on the object if it is an enum or Int unless the Int has a default value.</p>
  
  <p>Thanks for any advice I can get for getting all properties of a class no matter what they are.</p>