Serialization 获取Swift类的属性列表
我正在尝试获取swift类的属性列表。这里有一个类似的问题。除了某些类型之外,我的所有功能都正常运行,不会从Serialization 获取Swift类的属性列表,serialization,enums,swift,Serialization,Enums,Swift,我正在尝试获取swift类的属性列表。这里有一个类似的问题。除了某些类型之外,我的所有功能都正常运行,不会从class\u copyPropertyList返回。到目前为止,我测试过的是Int?和enum。下面我有一个我正在尝试的示例类 enum PKApiErrorCode: Int { case None = 0 case InvalidSignature = 1 case MissingRequired = 2 case NotLoggedIn = 3
class\u copyPropertyList
返回。到目前为止,我测试过的是Int?
和enum。下面我有一个我正在尝试的示例类
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类(在
公共类可序列化:NSObject,可打印{
重写公共变量说明:字符串{
返回“\(self.toDictionary())”
}
}
可序列化扩展{
public func toDictionary()->NSDictionary{
var aClass:AnyClass?=self.dynamicType
var propertiesCount:CUnsignedInt=0
让PropertiesInClass:UnsafemtablePointer=class\u copyPropertyList(aClass和propertiesCount)
var propertiesDictionary:NSMutableDictionary=NSMutableDictionary()
对于变量i=0;iNSData{
var dictionary=self.toDictionary()
变量错误:n错误?
返回NSJSONSerialization.dataWithJSONObject(字典,选项:NSJSONWritingOptions(0),错误:&err)
}
public func toJSONString()->NSString{
返回NSString(数据:self.toJSON(),编码:NSUTF8StringEncoding)
}
}
只有当选项是有效值时,字符串才会出现;如果对象是枚举或Int,则code
不会出现在对象上,除非Int具有默认值
感谢您对我获取类的所有属性(无论它们是什么)的建议。我在苹果开发者论坛上得到了关于这个问题的回复: class_copyPropertyList仅显示暴露于Objective-C运行时的属性。Objective-C不能表示非引用类型的Swift枚举或选项,因此这些属性不会暴露于Objective-C运行时
因此,总而言之,目前不可能使用这种方法将对象序列化为JSON。您可能需要研究使用其他模式来实现这一点,例如为每个对象指定序列化任务,或者可能使用将对象序列化为JSON。有人知道从何处开始,或者有人需要吗更多信息?我有一个完全相同的问题,使用相同的要点。这个问题已经讨论了一个月了。有什么更新吗?我一直摇头,认为必须有一个简单的方法来序列化swift类,但我还没有找到一个可以端到端工作的方法。
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)
}
}