立即将所有Realm对象转换为Dictionary

时间:2016-10-24 11:15:46

标签: ios swift dictionary nsdictionary realm

我正在使用Realm,我使用 # I used -200 as the limit to distinguish -151.30 from -494.42 (`sort(dat$ratio2, na.last = T)[1:2])`) limited_val <- c(-200, seq(-100, -1.0E-6, length = 3), seq(1.0E-6, r_range[2], length = 3)) ggplot(data = dat, aes(x = ACC, y = variable)) + geom_tile(aes(fill = ratio2), colour = 'white') + theme(axis.text.x = element_text(angle = 60, hjust = 1, color="black"), legend.title = element_blank(), legend.position="top", legend.key.size = unit(2.5, "cm")) + scale_fill_gradientn(colours = mycol, values = rescale(limited_val), limits = c(-200, max(dat$ratio2, na.rm=T)), breaks = c(-100, -50, 0, 50), oob=squish, guide = guide_colorbar(nbin = 100)) 将我的Realm模型转换为extension,但我不知道如何一次转换所有Realm模型。我想知道如何在一个地方同时转换所有领域对象,以便我可以将该字典发送到API。 这是我的Realm对象模型和我使用的扩展:

Dictionary

扩展:

class OrderItemList: Object {

dynamic var dateCreated = NSDate()
let orderItems = List<OrderItem>()
}

class OrderItem: Object {

dynamic var name = " "
dynamic var amount = 0
dynamic var internalUnique = Int()
dynamic var isCompleted = false
} 

1 个答案:

答案 0 :(得分:2)

不幸的是,没有将一批Realm对象转换为字典的灵丹妙药。您需要查询所需的对象,然后遍历每个对象以生成它的序列化版本。

let realm = try! Realm()
var objectDictionaries = [NSDictionary]()

let allObjects = realm.objects(OrderItemList.self)
for object in allObjects {
    let dictionary = object.toDictionary()
    objectDictionaries.append(dictionary)
}

我希望能回答你的问题!