RealmSwift:xCode 9上的属性值无效

时间:2017-09-28 17:55:43

标签: ios swift realm xcode9

我在Podfile上更新pod版本时遇到错误。我在xCode 9上使用了最新版本的RealmSwift框架。这是我的模型:

import Foundation
import RealmSwift
import ObjectMapper

class Salesman: Object,Mappable,DataElementDelegate, ObjectDelegate {

   //MARK: - properties
   public dynamic var id: Int = 0
   public dynamic var name: String = ""
   public dynamic var amount: Int = 0
   public dynamic var updated_at_timestamp: Int = 0

   //MARK: - Init
   required convenience init?(map: Map) {
       self.init()
   }

   //MARK: - Mapping
   func mapping(map: Map) {
       id<-map["id"]
       name<-map["name"]
       amount<-map["amount"]
       updated_at_timestamp<-map["updated_at_timestamp"]
   }

   //MARK: - Realm methods
   override static func primaryKey()->String?{
       return "id"
   }

   //MARK: - Methods
   public func getID()->Int{
       return self.id
   }

   //MARK: - Data delegate

   func getAmount() -> String {
       return "\(self.amount)"
   }

   func getName() -> String {
       return self.name
   }

   func getLastUpdate() -> Int {
       return self.updated_at_timestamp
   }

}

现在当我尝试在我的文件中插入值时,我发生了这次崩溃'RLMException', reason: 'Invalid value '1000' for property 'Salesman.amount''

我写了插入代码:

//MARK: - Mocks Salesman
public func mocksSalesman()->[Salesman]{

    var array: [Salesman] = []

    let sale1 = Salesman()
    sale1.id = 1
    sale1.name = "Raul"
    sale1.amount = 1000

    let sale2 = Salesman()
    sale2.id = 2
    sale2.name = "Tomas"
    sale2.amount = 500

    let sale3 = Salesman()
    sale3.id = 3
    sale3.name = "Luciano"
    sale3.amount = 700

    array.append(sale1)
    array.append(sale2)
    array.append(sale3)

    return array

}

public func insert(){
    let list = self.mocksSalesman()
    let realm = try! Realm()
    for item in list{
        try! realm.write {
            realm.add(item, update: true)
        }
    }
}

我试图将3名推销员插入我的领域数据库,但我遇到了这个问题。

可能会发生什么?

此致

1 个答案:

答案 0 :(得分:0)

好的,我有答案。那就是这个代码:

func getAmount() -> Int {
   return self.amount
}

而不是:

func getAmount() -> String {
   return "\(self.amount)"
}