2014-09-02 136 views
1

我想获取swift类的属性列表。有类似的问题herehere。我有一切工作,除了某些类型不从class_copyPropertyList返回。到目前为止我测试过的那些是Int?和枚举。我正在尝试下面的示例类。获取Swift类的属性列表

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"] >>> { (object: JSON) -> PKApiErrorCode? in 
      if let c = object >>> JSONInt { 
       return PKApiErrorCode.fromRaw(c) 
      } 

      return nil 
     } 

     message = error["message"] >>> JSONString 
     userMessage = error["userMessage"] >>> JSONString 
    } 
} 

而且Serializable类(与https://gist.github.com/turowicz/e7746a9c035356f9483d一些帮助是

public class Serializable: NSObject, Printable { 
    override public var description: String { 
     return "\(self.toDictionary())" 
    } 
} 

extension Serializable { 
    public func toDictionary() -> NSDictionary { 
     var aClass : AnyClass? = self.dynamicType 
     var propertiesCount : CUnsignedInt = 0 
     let propertiesInAClass : UnsafeMutablePointer<objc_property_t> = class_copyPropertyList(aClass, &propertiesCount) 
     var propertiesDictionary : NSMutableDictionary = NSMutableDictionary() 
     for var i = 0; i < 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<Serializable> { 
       var subArray = Array<NSDictionary>() 
       for item in (propValue as Array<Serializable>) { 
        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() -> NSData! { 
     var dictionary = self.toDictionary() 
     var err: NSError? 
     return NSJSONSerialization.dataWithJSONObject(dictionary, options:NSJSONWritingOptions(0), error: &err) 
    } 

    public func toJSONString() -> NSString! { 
     return NSString(data: self.toJSON(), encoding: NSUTF8StringEncoding) 
    } 
} 

弦乐似乎如果选配的有效值和code从来没有在对象上似乎只出现,如果它是一个枚举或诠释,除非诠释有一个默认值。

感谢您的任何意见,我可以得到得到一个类的所有属性,不管它们是什么。

+0

是否有人甚至有线索从哪里开始,或者是否有人需要更多的信息? – smitt04 2014-09-08 14:10:29

+0

我有这个完全相同的问题,使用同样的要点。这是一个月在这个questi上。任何更新?我一直在摇头,认为必须有一种直接的方式来序列化一个快速的课程,但我还没有找到一个端到端的方法。 – 2014-10-15 22:34:46

回答

3

我在苹果开发者论坛上得到了关于这个问题的回复:

“class_copyPropertyList只显示暴露给Objective-C运行时的属性。使用该目标C不能代表夫特枚举或非引用类型的自选,所以这些属性不暴露于Objective-C运行。”

Source

因此,简言之,序列化到JSON方法目前不可行,您可能需要考虑使用其他模式来实现此目的,例如将序列化任务交给每个对象,或者使用reflect() method将对象序列化为JSON。