存储数据Thread 1: Fatal error: init(realm:schema:) has not been implemented
时出错
在我的按钮中,我想存储哪个用户选择过的数据,同时我想显示它
但是当我单击按钮存储数据时,出现错误
并且我确定我的数据中没有任何nil
我希望我对这个问题的解释清楚。
我的代码: 型号:
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")
}
}
我的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)
}
}
}
答案 0 :(得分:0)
您不需要实现上面所有的init()
功能。相反,请尝试以下操作:-
required init
函数,因为我假设您要添加这些函数以使其编译。通常不需要它们。convenience
之前添加关键字init
。self.init()
。因此您的整个初始化代码应如下所示。
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
}