Anzer Erkin
Anzer Erkin

Reputation: 29

ios: Realm swift throws RLMException for no reason

I try to use Realm to save objects into database with swift language, but always get an exception like this

'RLMException', reason: 'Table has no columns

I followed the rules from the internet, about how to make class according to Realm, but never got to solved this problem.

Object class that need to save to database:

import Foundation
import RealmSwift

class GoTCharacter: Object{ 
   @objc dynamic var name: String = ""
   @objc dynamic var gender: String = ""
   @objc dynamic var aliases: [String] = []

   convenience init(withName name: String, gender: String, aliases: [String]) {
      self.init()
      self.name = name
      self.gender = gender
      self.aliases = aliases
   }
}

Code that throws an exception(realm.add()):

let realm = try! Realm()
try! realm.write {
    realm.add(GoTCharacter(withName: "Jon Snow", gender: "Male", aliases: [
    "Lord Snow"]))
}

Upvotes: 0

Views: 373

Answers (1)

TylerP
TylerP

Reputation: 9829

Realm doesn't support array properties like that. Replace this:

@objc dynamic var aliases: [String] = []

with the following:

let aliases = List<String>()

and adjust your code to work with the new type for the aliases property, like in your convenience initializer:

convenience init(withName name: String, gender: String, aliases: [String]) {
    self.init()
    self.name = name
    self.gender = gender
    self.aliases.append(objectsIn: aliases)
}

Upvotes: 2

Related Questions