如何从Class-Swift实现序列化NSDictionary

moh*_*acs 6 objective-c nsdictionary swift

在Objective-C中,我使用以下代码将自定义类序列化为一个工作正常的字典.为了熟悉Swift,将Objective-C代码移植到Swift.但是我无法实现这一点,我如何使用Swift实现这一目标?

这就是我用Objective-C实现的方法

.h
#import <Foundation/Foundation.h>
#import <objc/runtime.h>

@interface NSObject (aClass_NSDictionary)
- (NSDictionary *)NSDictionaryFromClass;
@end

.m
@implementation NSObject (aClass_NSDictionary)

- (NSDictionary *)NSDictionaryFromClass {

    Class aClass = [self class];
    u_int propertiesCount;

    objc_property_t *propertiesInAClass = class_copyPropertyList(aClass, &propertiesCount);

    NSMutableDictionary *propertiesDictionary = [[NSMutableDictionary alloc] initWithCapacity:propertiesCount];

    for (int i = 0; i < propertiesCount; i++)
    {
        NSString *strAKey = [NSString stringWithCString:property_getName(propertiesInAClass[i])
                                               encoding:NSUTF8StringEncoding];

        [propertiesDictionary setValue:[self valueForKey:strAKey]
                                forKey:strAKey];
    }
    free(propertiesInAClass);
    return propertiesDictionary;
}
@end
Run Code Online (Sandbox Code Playgroud)

当我在swift中编写相同的代码时,我无法找到相同的代码[self class].

class class2dicti : NSObject {

    class func nsdictionaryFromAClass() -> NSDictionary {

        let aClass = self.classForCoder
        var propertiesCount : u_int
        let propertiesInAClass : objc_property_t = class_copyPropertyList(aClass, &propertiesCount)

        //return NSDictionary()
    }
}
Run Code Online (Sandbox Code Playgroud)

更新

到目前为止我尝试过:

    let aClass = self.classForCoder
    var propertiesCount : u_int
    let propertiesInAClass : objc_property_t = class_copyPropertyList(aClass, &propertiesCount)
Run Code Online (Sandbox Code Playgroud)

let aClass : AnyClass! = self.classForCoder()
Run Code Online (Sandbox Code Playgroud)

没有成功,仍然是相同的编译器错误"无法找到接受提供的参数的'__conversion'的重载"

关于下面的答案,我找到了这个解决方案并且它有效.基本上我已经为我的班级创建了扩展.

class myClass : NSObject {
    var propertyOne = "prop One"
    var propertyTwo = [1, 2, 3]
    var propertyThree = ["A":1, "B":2, "C":3]
}

extension myClass {
    func toDictionary() -> NSDictionary {

        var aClass : AnyClass? = self.dynamicType
        var propertiesCount : CUnsignedInt = 0
        let propertiesInAClass : UnsafePointer<objc_property_t> = class_copyPropertyList(aClass, &propertiesCount)

        var propertiesDictionary : NSMutableDictionary = NSMutableDictionary()

        for var i = 0; i < Int(propertiesCount); i++ {
            var strKey : NSString? = NSString(CString: property_getName(propertiesInAClass[i]), encoding: NSUTF8StringEncoding)
            propertiesDictionary.setValue(self.valueForKey(strKey), forKey: strKey)
        }
        return propertiesDictionary
    }
}
Run Code Online (Sandbox Code Playgroud)

现在这let myclazz = myClass().toDictionary()给了我NSDictionary.欢迎提出所有建议.

Sul*_*han 1

在 Swift 中,你从不使用类。您使用的类型:

var clazz: AnyClass? = self.dynamicType
class_copyPropertyList(clazz, nil)
Run Code Online (Sandbox Code Playgroud)

所有返回Class, 例如的 Obj-C 方法classForCoder都更新为返回 Swift 类型 ( AnyClass)。

请注意,您还有其他类型的问题:

var propertiesCount : CUnsignedInt = 0
let propertiesInAClass : UnsafePointer<objc_property_t> = class_copyPropertyList(clazz, &propertiesCount)
Run Code Online (Sandbox Code Playgroud)

如果您实际上需要访问 Obj-C 类AnyObject,例如创建一个类数组并将其传递给 Obj-C,请参阅