เป็นเพียงแค่นี้จริงๆ:
let kBoat = try? yourContainer.viewContext.count(for: NSFetchRequest(entityName: "Boat"))
"เรือ" เป็นเพียงชื่อของเอนทิตีจากหน้าจอโมเดลข้อมูลของคุณ:

โลกyourContainerคืออะไร
หากต้องการใช้ข้อมูลหลักในแอปของคุณในบางครั้งคุณก็สามารถทำได้
var yourContainer = NSPersistentContainer(name: "stuff")
โดยที่ "stuff" เป็นเพียงชื่อของไฟล์โมเดลข้อมูล

คุณเพียงแค่มีซิงเกิลตันสำหรับเรื่องนี้
import CoreData
public let core = Core.shared
public final class Core {
    static let shared = Core()
    var container: NSPersistentContainer!
    private init() {
        container = NSPersistentContainer(name: "stuff")
        container.loadPersistentStores { storeDescription, error in
            if let error = error { print("Error loading... \(error)") }
        }
    }
    
    func saveContext() {
        if container.viewContext.hasChanges {
            do { try container.viewContext.save()
            } catch { print("Error saving... \(error)") }
        }
    }
}
ดังนั้นจากที่ใดก็ได้ในแอพ
core.container
เป็นภาชนะของคุณ
ดังนั้นในทางปฏิบัติเพื่อให้นับจำนวนเอนทิตีใด ๆ มันเป็นเพียง
let k = try? core.container.viewContext.count(for: NSFetchRequest(entityName: "Boat"))