小编iku*_*hub的帖子

访问 SwiftData 模型的关系属性时崩溃

我有两个一对多模型,在尝试访问模型的关系属性时遇到无法解释的崩溃。

错误原因如下:

线程 1:EXC_BREAKPOINT(代码=1,子代码=0x1038f4448)

Xcode 显示错误发生在模型的 .getValue(for: .rows) 方法中。 在此输入图像描述

这是我的 SwiftData 模型:

@Model class Row {
    var section: Section?
    
    init(section: Section? = nil) {
        self.section = section
    }
    init() {
        self.section = nil
    }
}

@Model class Section {
    @Relationship(.cascade, inverse: \Row.section)
    var rows: [Row] = []
        
    init(rows: [Row]) {
        self.rows = rows
    }
}

Run Code Online (Sandbox Code Playgroud)

这是我的代码:

class ViewController: UIViewController {
    var container: ModelContainer?

    override func viewDidLoad() {
        super.viewDidLoad()
        
        do {
            container = try ModelContainer(for: [Section.self, Row.self])
        } catch {
            print(error) …
Run Code Online (Sandbox Code Playgroud)

swift-data

11
推荐指数
1
解决办法
1130
查看次数

标签 统计

swift-data ×1