trong Objective-C, tôi đang sử dụng các mã sau để tuần tự hóa một lớp tùy chỉnh thành một từ điển hoạt động tốt. Để làm quen với Swift, hãy chuyển mã Objective-C sang Swift. Tuy nhiên tôi không thể đạt được điều này, làm thế nào để làm điều này với Swift?làm cách nào để nối tiếp NSDictionary từ một Class - Swift thực hiện
đây là cách tôi đạt được với 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
khi tôi viết cùng mã trong nhanh chóng tôi không thể quản lý để tìm hiểu tương đương với [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()
}
}
Cập nhật
cho đến nay tôi đã cố gắng:
let aClass = self.classForCoder
var propertiesCount : u_int
let propertiesInAClass : objc_property_t = class_copyPropertyList(aClass, &propertiesCount)
và
let aClass : AnyClass! = self.classForCoder()
không thành công, vẫn cùng một trình biên dịch báo lỗi "Không thể tìm thấy một tình trạng quá tải cho '__conversion' chấp nhận các đối số được cung cấp "
Giải pháp
liên quan đến câu trả lời bên dưới Tôi đã tìm thấy giải pháp này và nó đã hoạt động. Về cơ bản tôi đã tạo ra phần mở rộng cho lớp học của tôi.
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
}
}
giờ đây let myclazz = myClass().toDictionary()
mang lại cho tôi NSDictionary. tất cả các đề xuất đều được hoan nghênh.
bạn đã thử 'self.classForCoder' chưa? – Firo
let aClass: AnyClass! = self.classForCoder() cho tôi "Không thể tìm thấy quá tải cho '__conversion' chấp nhận đối số được cung cấp" – mohacs
xóa '()' nó phải là 'self.classForCoder' mà không có dấu ngoặc đơn. 'classForCoder' là thuộc tính. Bạn có thể 'classForCoder()' như là một phương thức lớp trên một lớp (không phải là một cá thể) ('MyClass.classForCoder()') – Firo