折腾:
期间,用代码:
import RealmSwift class RegionItem:NSObject { var id:Int var code:Int var name:String var parentId:Int var level:Int var regionOrder:Int var englishName:String var shortName:String … } var regionItemList = [RegionItem]() var realm = try! Realm() for eachRegionJson in dataJsonArr { gLog.verbose("eachRegionJson=\(eachRegionJson)") let curRegionItem: RegionItem = parseJsonToRegionItem(eachRegionJson) gLog.verbose("curRegionItem=\(curRegionItem)") try! realm.write { realm.add(curRegionItem) } regionItemList.append(curRegionItem) } |
结果
realm.add(curRegionItem)
出错:
然后在另外一个RegionItem定义的文件中,想要去改为Object,结果:
//class RegionItem:NSObject { class RegionItem: Object { |
报错:
Use of undeclared type ‘Object’
-》
好像此处swift中,没有Object,只有原先的NSObject
搜:
realm Cannot convert value of type to expected argument type ‘Object’
难道是:
需要调用的地方,去导入
import Realm
而不是原先的:
import RealmSwift
?
后来无意间发现:
在使用到Object的文件中,导入import RealmSwift:
import RealmSwift class RegionItem: Object { var id:Int var code:Int var name:String var parentId:Int var level:Int var regionOrder:Int var englishName:String var shortName:String |
即可,至少没有找不到Object的错误了
-》看来是:
RealmSwift中,定义了Object这个class
-》搞得我原以为Object是swift自带的呢。。。
然后也看到帖子:
ios – Swift cannot convert value of type to expected argument in Realm – Stack Overflow
中提到了此点。
[总结]
Object是RealmSwift定义的类,
导入
import RealmSwift
即可解决
class RegionItem: Object {
报:
Cannot convert value of type to expected argument type ‘Object’
的错误了。
转载请注明:在路上 » [已解决]swift中使用realm去write出错:Cannot convert value of type to expected argument type Object