Post

Replies

Boosts

Views

Activity

Reply to Multi-Selection List : changing Binding Array to Binding Set and back again
Absolutely, sorry for the opacity. First and foremost, the Pickerview takes a bound set as a parameter. I know while testing I am using a set as a parameter, but I really need to figure out how to redesign and take an array as a parameter instead. I believe the selection of colors has to be a set. So I will need to accept a Bound array, transform it into a set for use, and then back to an array again to get the bound value to update in my parent view... My reason for doing so is that all the rest of my data is Array based. So with hope of making this a very reusable piece of code, I either will need to make it accept an array, or I will have to do the opposite transformation dozens of times in the parent views. As to the other stuff... First whenever you add or delete colors using the Pickloader view, it doesn't update the list view until you exit and reload the view, or change a second item. Similarly, if you make changes, and then click the button at the bottom to reset to defaults, or clear the list entirely, it does what it says, but it does not update the view until the next change is made. It's like the view is always one step behind the actions. I am trying to use the list of colors stored in userDefaults directly, and I think I am going to need to copy it out, use it, and then save it back later... Not sure. With regard to deleting choices that are being used, this can be demonstrated by viewing the canvas preview with preset data. I have it set up for Red and Blue. When you run that preview, it works perfectly, setting up Red and Blue as the choices, and allowing you to edit them and returning the edited set. However, if you then were to delete one of the colors - red or blue - and go back, you will find that it is listed in the selection set still, but can't be deleted or changed. The only way to remove it currently would be to add it back in to the list of choices. My plan there was to eventually check any presets passed in to verify it is in the current list, and add anything in that isn't in the list. I think I can figure out the rest of this stuff, though I am certainly not turning away advice! May take me a while, but I think I know what to do there. I am drawing dead with respect to the first issue, though. I know it works right now, but I need to work with array's, not sets. I either will need to do this once here, or the reverse a dozen times over on the other side. For the life of me I cannot figure out how to get it to work. Thanks for the response ps I am trying to teach myself swift. I learned Cobol, fortran, and 6800 assembly back in the day, and been out of touch since. No sir I did not forget a zero on that number. Best way to learn is to do, so I have a Swift Data Model... Schools with classes full of teachers and students etc. Trying to put it all together. No other reason then to learn how to do it before I actually attempt something I care about.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jul ’25
Reply to SwiftData and discarding unsaved changes idea???
After many tries and different options, the best seems to be to create a private state temp variable of the same type as your @Bindable parameter. Then use .task{} to assign each of the values of the parameter to the temp variable. Edit away on the temp variable - and then when you are done, if you discard changes, simply navigate back. If you save changes, then copy temp variable back to the parameter before navigating back. So just like joadan said.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jul ’25
Reply to Complex Swift Data Relationships...
Another few small refinements - school delete rules set up I think import Foundation import SwiftData // Define School // Each School will offer a list of [Schoolclass] // Each Schoolclass will have one Teacher and a list of [Student] // Each Teacher may teach multiple classes, but only at one unique school // Each Student will attend multiple classes but only at one unique school // Bonus!! Start with the assumption that each SchoolClass instance is unique and only offered in one school, but want to expand so that a particular SchoolClass may be offered at more than one school, but have unique Teacher and [Student] for that class at each school @Model class School: Identifiable { var id: UUID = UUID() var name: String var mascot: String @Relationship(deleteRule: .cascade) var schoolClasses: [SchoolClass] // If a school is deleted, delete the classes associated with it, but leave the students and teachers all with an empty class list. // Every school that is created will have a class list, but it may be an empty list init (name: String, mascot: String = "", schoolClasses: [SchoolClass] = []) { self.name = name self.mascot = mascot self.schoolClasses = schoolClasses } } @Model class SchoolClass: Identifiable { var id: UUID = UUID() var name: String @Relationship(deleteRule: .cascade, inverse: \School.schoolClasses ) var school: School // If I delete a class, it will delete it from the list of classes offered by its associated school. // It should delete this class name from the list of classes a teacher is teaching, or the list of classes a each student is attending... @Relationship(deleteRule: .cascade) var teacher: Teacher? @Relationship(deleteRule: .cascade ) var students: [Student] //Every class will have a student list, but it may be empty. Teacher may exist or be nil init(id: UUID, name: String, school: School, teacher: Teacher, students: [Student] = []) { self.id = id self.name = name self.school = school self.teacher = teacher self.students = students } } @Model class Teacher: Identifiable { var id: UUID = UUID() var name: String var email: String @Relationship(deleteRule: .nullify, inverse: \SchoolClass.teacher ) var schoolClasses: [SchoolClass] //If a teacher is deleted, leave any class they are in, and replace teacher with nil //Every teacher created will have a class list, but it may be empty init(name: String, email: String, schoolClasses: [SchoolClass] = []) { self.name = name self.email = email self.schoolClasses = schoolClasses } } @Model class Student: Identifiable { var id: UUID = UUID() var name: String var email: String @Relationship(deleteRule: .cascade, inverse: \SchoolClass.students ) var schoolClasses: [SchoolClass] //If a student is deleted, remove them from the student listing of every class //Every Student created will have a class list, but it may be empty init(name: String, email: String, schoolClasses: [SchoolClass] = []) { self.name = name self.email = email self.schoolClasses = schoolClasses } }
Topic: Design SubTopic: General Tags:
Jun ’25
Reply to Complex Swift Data Relationships...
Another day of refining this model gets me to the following code... Please see if my comments are correct, and if I am missing something... import Foundation import SwiftData // Define School // Each School will offer a list of [Schoolclass] // Each Schoolclass will have one Teacher and a list of [Student] // Each Teacher may teach multiple classes, but only at one unique school // Each Student will attend multiple classes but only at one unique school // Bonus!! Start with the assumption that each SchoolClass instance is unique and only offered in one school, but want to expand so that a particular SchoolClass may be offered at more than one school, but have unique Teacher and [Student] for that class at each school @Model class School: Identifiable { var id: UUID = UUID() var name: String var mascot: String var schoolClasses: [SchoolClass] // Every school that is created will have a class list, but it may be an empty list init (name: String, mascot: String = "", schoolClasses: [SchoolClass] = []) { self.name = name self.mascot = mascot self.schoolClasses = schoolClasses } } @Model class SchoolClass: Identifiable { var id: UUID = UUID() var name: String @Relationship(deleteRule: .cascade, inverse: \School.schoolClasses ) var school: School // If I delete a class, it will delete it from the list of classes offered by its associated school. var teacher: Teacher? var students: [Student] //Every class will have a student list, but it may be empty. Teacher may exist or be nil init(id: UUID, name: String, school: School, teacher: Teacher, students: [Student] = []) { self.id = id self.name = name self.school = school self.teacher = teacher self.students = students } } @Model class Teacher: Identifiable { var id: UUID = UUID() var name: String var email: String @Relationship(deleteRule: .nullify, inverse: \SchoolClass.teacher ) var schoolClasses: [SchoolClass] //If a teacher is deleted, leave any class they are in, and replace teacher with nil //Every teacher created will have a class list, but it may be empty init(name: String, email: String, schoolClasses: [SchoolClass] = []) { self.name = name self.email = email self.schoolClasses = schoolClasses } } @Model class Student: Identifiable { var id: UUID = UUID() var name: String var email: String @Relationship(deleteRule: .cascade, inverse: \SchoolClass.students ) var schoolClasses: [SchoolClass] //If a student is deleted, remove them from the student listing of every class //Every Student created will have a class list, but it may be empty init(name: String, email: String, schoolClasses: [SchoolClass] = []) { self.name = name self.email = email self.schoolClasses = schoolClasses } } I also am curious if anyone has any observations on the following... Does it matter putting @Relationship everywhere to explicitly specify a delete rule? Did I use it correctly? specifically what happens if I delete a school, will it behave as I commented in code? How to enforce the teachers and students are only associated with one school any changes I should make to be able to associate a class with multiple schools, but have a separate teacher and student list for each school? The thought process is to offer a "transcript" for a student so that if they move from one school to another, they are credited for the class. The real idea is simply for me to figure out how to do many to many relationships. Thanks again to anyone who comments back. I get the single one to many relationship ok, but I cannot find much on complicated relationships like this.
Topic: Design SubTopic: General Tags:
Jun ’25
Reply to Complex Swift Data Relationships...
Thanks... That is exactly the type of feedback I am looking for... Again, not a real app, but thinking of how to make it easiest to get a class roster, a list of all students for a class or for a school, a list of all teachers for a school, etc. Is this affected later from making this change?
Topic: Design SubTopic: General Tags:
Jun ’25
Reply to Landmarks Tutorial. Preview doesn't work
A week of fighting with this issue, then I happened to notice the debugger, which had a line indicating the data file did not load. I replaced my "ModelData file with the one from the finished sample project, where landmark is defined and loaded from json file, and poof! Everything Works! I have no idea what typo I had, but obviously I mucked something up.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’24