我收到这个错误...
2017-03-26 17:34:49.104919 Mobile[518:254067] ***
由于未捕获的异常“RLMException”而终止应用程序,
原因:“只能将“字符串”和“整数”属性指定为主键”
在预填充的领域中,我有所有字符串列,除了 2 个用于 lat、lng 的 Double 列。
这是我的模型:
import Foundation
import RealmSwift
class Destination: Object{
dynamic var destinationSlackChannelName = ""
dynamic var destinationSlackChannelId = ""
dynamic var destinationName = ""
dynamic var destinationType = ""
dynamic var destinationCode = ""
dynamic var destinationRegionCode = ""
dynamic var destinationSiteSlackChannelName = ""
dynamic var destinationCity = ""
dynamic var destinationCountry = ""
dynamic var destinationStatus = ""
dynamic var destinationLastUpdated = ""
dynamic var lat:Double = 0.0
dynamic var lng:Double = 0.0
}
以下是我在一个名为 RealmManager 的单例中配置和查询领域的方式......
func getHebronDestinations() -> Results<Destination> {
let bundleUrl = Bundle.main.url(forResource: "default", withExtension: "realm")
let config = Realm.Configuration(
fileURL: bundleUrl,
readOnly: true,
schemaVersion: 0,
migrationBlock: { migration, oldSchemaVersion in
if (oldSchemaVersion < 1) {
Log.info?.message("\(oldSchemaVersion)")
Log.info?.message("wtf")
}
if (oldSchemaVersion < 2) {
Log.info?.message("\(oldSchemaVersion)")
}
Log.info?.message("Realm migration did run") // Log to know migration was executed
}
)
let realm = try! Realm(configuration: config)
let naoHebronResults = realm
.objects(Destination.self)
//.filter("destinationRegionCode == 'nao' AND destinationCode == 'heb'")
Log.info?.message("\(naoHebronResults)")
for res in naoHebronResults{
Log.info?.message(res.destinationName)
}
return naoHebronResults
}
这是 ViewController 中的函数调用...
override func viewDidLoad() {
super.viewDidLoad()
print("viewDidLoad")
let realmManager = RealmManager.shared
let hebDevices = realmManager.getHebronDestinations()
print(hebDevices)
}
如果模型中没有主键,为什么我仍然会收到主键错误?
我通过领域浏览器从 csv 制作了这个领域文件……它以前工作过一次。