如何从类序列化 NSDictionary - Swift 实现 [英] how do I serialise NSDictionary from a Class - Swift implementation

查看:50
本文介绍了如何从类序列化 NSDictionary - Swift 实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

in Objective-C, I am using following codes to serialise a custom class to a dictionary which working fine. In order to being familiar to Swift, porting Objective-C codes to Swift. However I couldn’t achieve this one, how do I make this with Swift?

这就是我用 Objective-C 实现的

this is how I achieve with 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

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

when I wrote same code in swift i couldn’t manage to find out equivalent of [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()
    }
}

更新

到目前为止我已经尝试过:

Update

so far i have tried:

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

let aClass : AnyClass! = self.classForCoder()

没有成功,仍然是同样的编译器错误找不到接受所提供参数的‘__conversion’的重载"

no success, still same compiler error "Could not find an overload for '__conversion' that accepts the supplied arguments"

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

regarding to answers below I found this solution and it worked. Basically I have created extension for my class.

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
    }
}

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

now this let myclazz = myClass().toDictionary() gives me the NSDictionary. all suggestions are welcome.

推荐答案

在 Swift 中,您从不使用类.您使用类型:

In Swift you never work with classes. You work with types:

var clazz: AnyClass? = self.dynamicType
class_copyPropertyList(clazz, nil)

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

All Obj-C methods that return a Class, e.g. classForCoder are updated to return a Swift type instead (AnyClass).

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

Note that you have other type problems there:

var propertiesCount : CUnsignedInt = 0
let propertiesInAClass : UnsafePointer<objc_property_t> = class_copyPropertyList(clazz, &propertiesCount)

如果您确实需要以 AnyObject 的形式访问 Obj-C 类,例如创建类数组并将其传递给 Obj-C,请参阅 这个

If you actually need to access the Obj-C class as AnyObject, for example to create an array of classes and pass it to Obj-C, see this

这篇关于如何从类序列化 NSDictionary - Swift 实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆