You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+10-10Lines changed: 10 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,7 +46,6 @@ We create our `Note` entity programmatically:
46
46
```swift
47
47
@objc(Note)
48
48
finalclassNote: NSManagedObject, Identifiable {
49
-
50
49
@NSManagedvar id: UUID
51
50
@NSManagedvar text: String
52
51
@NSManagedvar created: Date
@@ -72,15 +71,18 @@ let container = try await NSManagedObjectModel(Note.self)
72
71
73
72
-[Example_02](https://github.com/anconaesselmann/ProgrammaticCoreData/tree/main/Examples/Example_02) is a Book Archive app with a to-many relationship from an `Author` to their `Book`s and an inverse to-one relationship from a `Book` and it's `Author`:
74
73
75
-
We create an `Author` entity programmatically. Note the `addToBooks` and `removeFromBooks` function signatures. We are exposing objective-c methods that get generated for us to add to our `_books` ordered set:
74
+
We create an `Author` entity programmatically:
76
75
```swift
77
76
@objc(Author)
78
77
finalclassAuthor: NSManagedObject, Identifiable {
79
-
80
78
@NSManagedvar id: UUID
81
79
@NSManagedvar name: String
82
80
@NSManagedvar _books: NSOrderedSet
83
81
82
+
// CoreData requires an `NSSet` or `NSOrderedSet` for relationships. In this example we
83
+
// use `_` to "hide" the inner none-type-safe objective-c workings and we only
84
+
// expose a read-only Swift array of `Book`s. We add to `_books` with the auto-generated
85
+
// methods below.
84
86
var books: Array<Book> {
85
87
get { _books.arrayas!Array<Book> }
86
88
}
@@ -90,16 +92,10 @@ final class Author: NSManagedObject, Identifiable {
90
92
91
93
@objc(remove_booksObject:)
92
94
@NSManagedfuncremoveFromBooks(_value: Book)
93
-
94
-
@objc(add_books:)
95
-
@NSManagedfuncaddToBooks(_values: Set<Book>)
96
-
97
-
@objc(remove_books:)
98
-
@NSManagedfuncremoveFromBooks(_values: Set<Book>)
99
95
}
100
96
```
101
97
102
-
We create a `Book` entity programmatically. Note that it should be impossible to create a book without adding the book to it's authors `books` property, since we have an inverse relationship. We achieve this by creating an initializer that adds the book to it's `Author` and marking all other initializers as `unavailable`:
98
+
We create a `Book` entity programmatically. Note that it should be impossible to create a book without adding the book to it's authors `books` property, since we have an inverse relationship:
103
99
```swift
104
100
@objc(Book)
105
101
finalclassBook: NSManagedObject, Identifiable {
@@ -108,6 +104,9 @@ final class Book: NSManagedObject, Identifiable {
108
104
@NSManagedvar title: String
109
105
@NSManagedvar author: Author
110
106
107
+
// The only available initializer initializes all none-optional properties.
108
+
// Below we mark all other initializers as unavailable. The compiler can now
109
+
// enforce that we never instantiate an instance without setting all propperties.
111
110
init(
112
111
context: NSManagedObjectContext,
113
112
id: UUID,
@@ -121,6 +120,7 @@ final class Book: NSManagedObject, Identifiable {
0 commit comments