ahmed ali
ahmed ali

Reputation: 7

Realm Error (Thread 1: Fatal error: init(realm:schema:) has not been implemented)

I got error when I stored data Thread 1: Fatal error: init(realm:schema:) has not been implemented in my button I want storage the data which user has been choce it and at the same time I want display it but when I click on the button to storage my data I got error and I'm sure there is not any nil in my data I hope that my explanation of the problem is clear .

my code : model :

import Foundation
import UIKit
import RealmSwift
import Realm
class Foods : Object {
@objc dynamic var name = ""
@objc dynamic var price = 0.0
@objc dynamic var descriptionn = ""
@objc dynamic var time = ""
@objc dynamic var rating = 0.0
 var image:UIImage?
@objc dynamic var count = 0.0

init(name : String,price : Double , count:Double ,description : String,time : String,rating : Double,image : UIImage) {
    self.name = name
    self.price = price
    self.descriptionn = description
    self.time = time
    self.rating = rating
    self.image = image
    self.count = count
    super.init()
}

init(name : String,price : Double) {
    self.name = name
    self.price = price

    super.init()
}

required init() {
    name = ""
    price = 0.0
    super.init()
}


required init(realm: RLMRealm, schema: RLMObjectSchema) {
    fatalError("init(realm:schema:) has not been implemented")
}

required init(value: Any, schema: RLMSchema) {
    fatalError("init(value:schema:) has not been implemented")
}



 }

my UITableViewCell :

import UIKit
import HCSStarRatingView
import GMStepper
import RealmSwift

class FoodsSecoundTableViewCell: UITableViewCell {
@IBOutlet weak var foodTitle: UILabel!
@IBOutlet weak var foodPrice: UILabel!
@IBOutlet weak var foodRating: HCSStarRatingView!
@IBOutlet weak var foodImage: UIImageView!
@IBOutlet weak var steperCount: GMStepper!
@IBOutlet weak var btnSend: UIButton!
var result : Double?
let realm = try! Realm()

weak var delegate: FoodsSecoundTableViewCellDelegate?

override func awakeFromNib() {
    super.awakeFromNib()
    btnSend.layer.cornerRadius = 23
    btnSend.layer.borderColor = UIColor.white.cgColor

    if steperCount.value == 0 {
    btnSend.isEnabled = false

    } else if steperCount.value > 0{
        btnSend.isEnabled = true

    }


}


override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
}
@IBAction func myStepper(_ sender: GMStepper) {

    delegate?.stepper(sender, at: sender.tag, didChangeValueTo: sender.value)


}


@IBAction func btnSendIt(_ sender: Any) {

    print(foodPrice.text)
    let myFood = Foods(name: foodTitle.text!, price: Double(foodPrice.text!)!)
    try! realm.write {
        realm.add(myFood)
    }

    let  result = realm.objects(Foods.self)

    for food in result {
        print(food.name)
        print(food.price)


    }

}

}

Upvotes: 0

Views: 674

Answers (1)

Chris Shaw
Chris Shaw

Reputation: 1610

You don't need to implement all the init() functions you have above. Instead, try this:-

  • Remove the required init functions, as I assume you are adding these to make it compile. They're not normally needed.
  • For the initialisers that you want to implement (which I assume are the first two above), add the keyword convenience before init.
  • Use self.init() at the start of your own initialisers.

So your entire initialiser code should be as below.

convenience init(name : String,price : Double , count:Double ,description : String,time : String,rating : Double,image : UIImage) {
    self.init()
    self.name = name
    self.price = price
    self.descriptionn = description
    self.time = time
    self.rating = rating
    self.image = image
    self.count = count
}

convenience init(name : String,price : Double) {
    self.init()
    self.name = name
    self.price = price    
}

Upvotes: 1

Related Questions