2016-12-28 33 views
0

與Realm中的以下Swift 3.0對象相當的是什麼?Swift 3.0.2中的一對多對象Realm中的對象

定期斯威夫特

商店類別:

import Foundation 
class Store{ 
    var storeName = "" 
    var itemList = [Item]() 
} 

項目類:

import Foundation 
class Item{ 
    var itemName: String = "" 
    var price: Double = 0 
} 

境界

我試過,但我得到一個錯誤:

import Foundation 
import RealmSwift 

class Store:Object{ 
    dynamic var storeName = "" 
    dynamic var itemList = List<Item>() // here I get the error 
} 

Error: Property cannot be marked dynamic because its type cannot be represented in Objective-C

項目類:沒有錯誤

import Foundation 
import RealmSwift 

class Item : Object{ 

    dynamic var itemName: String = "" 
    dynamic var price: Double = 0 
} 
+1

你不需要聲明itemList爲動態proprtty – Coyote

回答

1

繼境界斯威夫特文檔的To-Many Relationships節的例子,你的類應該聲明爲:

class Store: Object { 
    dynamic var storeName = "" 
    let itemList = List<Item>() 
} 

dynamic修飾符不能用於List<T>屬性,因爲它要求ty該屬性的pe可以在Objective-C中表示,而Swift的泛型則不是。

+0

就是這樣,非常感謝! –