Posts

Post not yet marked as solved
1 Replies
140 Views
I know that this has been posted many times, but I am facing an issue where I can't save a model instance. I know that this is due to enums from others trying to find solutions. The error that shows: CoreData: error: Row (pk = 2) for entity 'ResidentInfo' is missing mandatory text data for property 'name' Solutions that I've tried: removed all enums from their respective structs to being standalone. making sure all enum calls use the full keyPath removing CustomStringConvertible from all enums and moving them to rawValue strings updating Xcode to 15.4b probably other things I've forgotten at this point File is too long to fit in post or comments (I'm sorry it's insanely long, it's a complex model), so I'll try to fit what I can where, are there anythings that stand out that may be an issue? Enums: enum Gender: String, CaseIterable, Codable { case male = "Male" case female = "Female" case transmale = "Trans Male" case transfemale = "Trans Female" case nonbinary = "Nonbinary" case other = "Other" } enum BodyBuild: String, CaseIterable, Codable { case small = "Small" case medium = "Medium" case large = "Large" } enum Status: String, Codable { case incomplete, complete } enum HairColor: String, CaseIterable, Codable { case black = "Black" case blonde = "Blonde" case dirtyBlonde = "Dirty Blonde" case brown = "Brown" case lightBrown = "Light Brown" case darkBrown = "Dark Brown" case red = "Red" } enum EyeColor: String, CaseIterable, Codable { case blue = "Blue" case brown = "Brown" case black = "Black" case green = "Green" case hazel = "Hazel" case gray = "Gray" case heterochromatic = "Heterochromatic" } enum RaceEthnicity: String, CaseIterable, Codable { case native = "American Indian or Alaska Native" case asian = "Asian" case black = "Black or African American" case pi = "Native Hawaiian or Other Pacific Islander" case white = "White" case mixed = "2 or More Races/Mixed" } enum MarkType: String, CaseIterable, Codable { case birthMark = "Birth Mark" case scar = "Scar" case tattoo = "Tattoo" case piercing = "Piercing" case other = "Other" } enum heightFormatStyle: String { case short, long, hash } enum suicideRiskLevel: String, Codable { case low = "Low" case medium = "Medium" case high = "High" } enum MedicationFrequency: String, CaseIterable, Codable { case daily = "Daily" case weekly = "Weekly" case biweekly = "Bi-Weekly" case monthly = "Monthly" case asNeeded = "As Needed" case temporary = "Temporary Use" case other = "Other" } enum SchoolOption: String, CaseIterable, Codable { case ged = "GED" case communityCollege = "Community College" case university = "4-Year College" } enum FamilyDiseaseTypes: String, CaseIterable, Codable { case alcoholism = "Alcoholism" case asthma = "Asthma" case cancer = "Cancer" case drugAbuse = "Drug Abuse" case hypertension = "High Blood Pressure" case nervousBreakdown = "Nervous Breakdown" case sca = "Sickle Cell Anemia" case seizures = "Convulsions, Seizures Epilepsy" case allergies = "Allergies" case birthDefect = "Birth Defect" case diabetes = "Diabetes" case heartDisease = "Heart Disease" case migraines = "Migraine Headaches" case obesity = "Obesity" case tuberculosis = "Tuberculosis" case thyroid = "Glandular/Thyroid Issues" } Structs: struct IdentifyingInformation: Codable { var hairColor: HairColor = HairColor.black var height: Height = Height(feet: 5, inches: 8) var race: RaceEthnicity = RaceEthnicity.native var build: BodyBuild = BodyBuild.small var eyeColor: EyeColor = EyeColor.blue var hispanic: Bool = false var distinguishingMarks: [DistinguishingMark] = [] var currentLivingSitutation: String = "" var currentSupportServices: String = "" struct DistinguishingMark: Codable, Identifiable { var id: UUID = UUID() var type: MarkType = MarkType.tattoo var location: String = "" var description: String = "" var photos: [Data] = [] } struct Height: Codable { var feet: Int var inches: Int func formatted(formatStyle: HeightFormatStyle = .short) -> String { switch formatStyle { case HeightFormatStyle.hash: return "\(feet)'\(inches)\"" case HeightFormatStyle.long: return "\(feet) feet \(inches) inches" case HeightFormatStyle.short: return "\(feet)ft \(inches)in" } } } } struct EmergencyNeeds: Codable { var food: Bool = false var shelter: Bool = false var clothing: Bool = false var hygeine: Bool = false var medical: Bool = false var mentalHealth: Bool = false var otherNeeds: String = "" var abuseNeglectAllegations: Bool = false var abuseReport: AbuseReport? = nil struct AbuseReport: Codable { var reportAccepted: Bool = false var whoAccepted: String = "" var reportNumber: String = "" } } struct RiskAssessment: Codable { var selfHarm: Bool = false var selfHarmAttempt: Bool = false var selfHarmExplanation: String? = nil var currentFeelings: String? = nil var dangerToOthers: Bool = false var dangerToOthersExplanation: String? = nil var suicideAssessmentRequired: Bool { return selfHarm || dangerToOthers } } struct SuicideAssessment: Loopable, Codable { var dateCompleted: Date = Date() var familyMember: Bool = false var treatedMentalIllness: Bool = false var chronicIllness: Bool = false var historyOfSelfDestructiveBehavior: Bool = false var recentLoss: Bool = false var isolatedAloneHopeless: Bool = false var goodbyeNoteOrPosessionDelegation: Bool = false var plan: Bool = false var betterOff: Bool = false var wishToDie: Bool = false var manualReview: Bool = false var comments: String = "" var helpCallMade: Bool = false var riskLevel: suicideRiskLevel { let riskScore = try? allProperties().reduce(0) { result, property in if let isTrue = property.value as? Bool, isTrue { return result + 1 } else { return result } } switch riskScore! { case 0 ... 3: return .low case 4 ... 7: return .medium default: return .high } } } [Continued in comments]
Posted Last updated
.
Post not yet marked as solved
1 Replies
846 Views
I have multiple classes that are contained within one another. Initializing class A(no other classes as properties) has no issues, but the moment that it tries to initialize class B, which takes class A as a property, an EXC_BAD_ACCESS error is thrown while attempting to set the first property value. Class A: import Foundation import SwiftData @Model public class Service: Identifiable, Equatable, Codable { @Attribute(.unique) public var id: UUID public var title: String public var price: Int public var stripePriceId: String public var servicePhoto: String public var serviceLength: Int public var category: [String] init(id: UUID, title: String, price: Int, stripePriceId: String, servicePhoto: String, serviceLength: Int, category: Array<String>) { self.id = id self.title = title self.price = price self.stripePriceId = stripePriceId self.servicePhoto = servicePhoto self.serviceLength = serviceLength self.category = category } } Class B: import Foundation import SwiftData import SwiftUI @Model public class ServiceOrder: Identifiable, Codable, Equatable { public var id: UUID @Relationship(.noAction) public var service: Service public var quantity: Int = 1 public var subtotal: Int { return service.price * quantity } public init(id: UUID = UUID(), service: Service, quantity: Int) { self.id = id self.service = service self.quantity = quantity } public func getValue<T>(for key: KeyPath<ServiceOrder, T>) -> T { return self[keyPath: key] } // This is where the error is being thrown. The custom setValue and getValue methods were added to every class to mitigate an 'Ambiguous use of __Value()' error, but that's for another thread public func setValue<T>(for key: ReferenceWritableKeyPath<ServiceOrder, T>, to newValue: T) { self[keyPath: key] = newValue } // This was added to see if following the Builder pattern might mitigate the issue by ensuring that all properties were initialized before being passed in, it did not change anything class ServiceOrderBuilder { private var id: UUID? private var service: Service? private var quantity: Int? init() { } init(id: UUID = UUID(), service: Service, quantity: Int = 1) { self.id = id self.service = service self.quantity = quantity } func setId(id: UUID = UUID()) -> ServiceOrderBuilder { self.id = id return self } func setService(service: Service) -> ServiceOrderBuilder { self.service = service return self } func setQuantity(quantity: Int = 1) -> ServiceOrderBuilder { self.quantity = quantity return self } func build() -> ServiceOrder? { guard let id = id, let service = service, let quantity = quantity else { return nil } return ServiceOrder(id: id, service: service, quantity: quantity) } } Here's where I'm doing the initialization of everything. I'm trying to just create sample data. import SwiftData import Foundation @MainActor public let previewContainer: ModelContainer = { do { var container = try ModelContainer( for: [Event.self, Braider.self, Queue.self, QueueSlot.self, Cart.self, ServiceOrder.self], ModelConfiguration(inMemory: true) ) var context = container.mainContext var serviceSampleData: [Service] = [ Service( id: UUID(), title: "Event Braid", price: 20, stripePriceId: "", servicePhoto: "", serviceLength: 15, category: []), ..., ] serviceSampleData.forEach { service in context.insert(service) } // This is where the error is thrown, no issue initializing the above Services var serviceOrder1: ServiceOrder = ServiceOrder( service: serviceSampleData[0], quantity: 1) context.insert(serviceOrder1) // ...continue building higher classes/Models that utilize the lower ones return container } catch { print("Failed to create container") do { return try ModelContainer( for: [Event.self], ModelConfiguration(inMemory: true) ) } catch { fatalError() }} }() I assumed that this was a SwiftData issue, as I mentioned in response to this post but looking back this issue has been happening for 7+ years, so now I'm assuming I'm doing something incorrect.
Posted Last updated
.
Post marked as solved
4 Replies
1.7k Views
Trying to utilize the new OpenAPI Generator, but after following all of the steps exactly as stated in the video, I get the following errors: - The package product 'SwiftSyntax' requires minimum platform version 13.0 for the iOS platform, but this target supports 12.0 - The package product 'SwiftSyntaxBuilder' requires minimum platform version 13.0 for the iOS platform, but this target supports 12.0 - The package product 'SwiftFormat' requires minimum platform version 13.0 for the iOS platform, but this target supports 12.0 - The package product 'SwiftFormatConfiguration' requires minimum platform version 13.0 for the iOS platform, but this target supports 12.0 under both _OpenAPIGeneratorCore and swift-openapi-generator I've seen other things online where people have had similar issues, but the fixes haven't been particularly relevant that I could tell.
Posted Last updated
.