Post

Replies

Boosts

Views

Activity

Reply to Picker using SwiftData
The model appears to have not pasted correctly, I used the code block. So here it is again. import SwiftData //Model one: type of contract, i.e. Firm Fixed Price, etc @Model final class TypeOfContract { var contracts: [Contract] @Attribute(.unique) var typeName: String @Attribute(.unique) var typeCode: String var typeDescription: String init(contracts: [Contract], typeName: String = "", typeCode: String = "", typeDescription: String = "") { self.contracts = contracts self.typeName = typeName self.typeCode = typeCode self.typeDescription = typeDescription } } //Model two: the Contract @Model final class Contract { var contractType: TypeOfContract? var costReports: [CostReport] @Attribute(.unique) var contractNumber: String @Attribute(.unique) var contractName: String var startDate: Date var endDate: Date var contractValue: Decimal var contractCompany: String var contractContact: String var contactEmail: String var contactPhone: String var contractNotes: String init(contractType: TypeOfContract? = nil, costReports: [CostReport], contractNumber: String = "", contractName: String = "", startDate: Date = .now, endDate: Date = .now, contractValue: Decimal = 0.00, contractCompany: String = "", contractContact: String = "", contactEmail: String = "", contactPhone: String = "", contractNotes: String = "") { self.contractType = contractType self.costReports = costReports self.contractNumber = contractNumber self.contractName = contractName self.startDate = startDate self.endDate = endDate self.contractValue = contractValue self.contractCompany = contractCompany self.contractContact = contractContact self.contactEmail = contactEmail self.contactPhone = contactPhone self.contractNotes = contractNotes } } //Model Three: The Cost Reports @Model final class CostReport { var contract: Contract? var periodStartDate: Date var periodEndDate: Date var bCWP: Double //Budgeted Cost Work Performed var aCWP: Double //Actual Cost Work Performed var bCWS: Double //Budgeted Cost Work Scheduled //Calculated fields init(contract: Contract? = nil, periodStartDate: Date = .now, periodEndDate: Date = .now, bCWP: Double = 0.0, aCWP: Double = 0.0, bCWS: Double = 0.0) { self.contract = contract self.periodStartDate = periodStartDate self.periodEndDate = periodEndDate self.bCWP = bCWP self.aCWP = aCWP self.bCWS = bCWS } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Oct ’25
Reply to Present User an error message when SwiftData save fails
import SwiftData //Model one: type of contract, i.e. Firm Fixed Price, etc @Model final class TypeOfContract { var contracts: [Contract] var typeName: String var typeCode: String var typeDescription: String init(contracts: [Contract], typeName: String = "", typeCode: String = "", typeDescription: String = "") { self.contracts = contracts self.typeName = typeName self.typeCode = typeCode self.typeDescription = typeDescription } } //Model two: the Contract @Model final class Contract { var contractType: TypeOfContract? var costReports: [CostReport] var contractNumber: String var contractName: String var startDate: Date var endDate: Date var contractValue: Double var contractCompany: String var contractContact: String var contactEmail: String var contactPhone: String var contractNotes: String init(contractType: TypeOfContract? = nil, costReports: [CostReport], contractNumber: String = "", contractName: String = "", startDate: Date = .now, endDate: Date = .now, contractValue: Double = 0.0, contractCompany: String = "", contractContact: String = "", contactEmail: String = "", contactPhone: String = "", contractNotes: String = "") { self.contractType = contractType self.costReports = costReports self.contractNumber = contractNumber self.contractName = contractName self.startDate = startDate self.endDate = endDate self.contractValue = contractValue self.contractCompany = contractCompany self.contractContact = contractContact self.contactEmail = contactEmail self.contactPhone = contactPhone self.contractNotes = contractNotes } } //Model Three: The Cost Reports @Model final class CostReport { var contract: Contract? var periodStartDate: Date var periodEndDate: Date var bCWP: Double //Budgeted Cost Work Performed var aCWP: Double //Actual Cost Work Performed var bCWS: Double //Budgeted Cost Work Scheduled //Calculated fields init(contract: Contract? = nil, periodStartDate: Date = .now, periodEndDate: Date = .now, bCWP: Double = 0.0, aCWP: Double = 0.0, bCWS: Double = 0.0) { self.contract = contract self.periodStartDate = periodStartDate self.periodEndDate = periodEndDate self.bCWP = bCWP self.aCWP = aCWP self.bCWS = bCWS } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Oct ’25
Reply to Save SwiftData object for model with one to many relationship
import SwiftData //Model for Earned Value Analysis The Model @Model final class CostReport{ var aCWP: Double //Actual Cost of Work Performed var bCWP: Double //Budgeted Cost of Work Performed var bCWS: Double // Budgeted Cost of Work Scheduled var cumACWP: Double// Cumlative Actual Cost of Work Performed var cumBCWP: Double // Cumlative Budgeted Cost of Work Performed var cumBCWS: Double // Cumlative Budgeted Cost of Work Scheduled var startDateOfPeriod: Date var endDateOfPeriod: Date var contract: Contract? init(aCWP: Double = 0.0, bCWP: Double = 0.0, bCWS: Double = 0.0, cumACWP: Double = 0.0, cumBCWP: Double = 0.0, cumBCWS: Double = 0.0, startDateOfPeriod: Date = .now, endDateOfPeriod: Date = .now, contract: Contract) { self.aCWP = aCWP self.bCWP = bCWP self.bCWS = bCWS self.cumACWP = cumACWP self.cumBCWP = cumBCWP self.cumBCWS = cumBCWS self.startDateOfPeriod = startDateOfPeriod self.endDateOfPeriod = endDateOfPeriod self.contract = contract } } @Model //Model for Contracts final class Contract{ var costReports: [CostReport] var contractType: ContractType? @Attribute(.unique)var contractNumber: String @Attribute(.unique)var contractName: String var startDate: Date var endDate: Date var contractValue: Double var contractorName: String var contractorContact: String var contractorPhone: String var contractorEmail: String init(costReports: [CostReport], contractType: ContractType, contractNumber: String = "", contractName: String = "", startDate: Date = .now, endDate: Date = .now, contractValue: Double = 0.0, contractorName: String = "", contractorContact: String = "", contractorPhone: String = "", contractorEmail: String = "") { self.costReports = costReports self.contractType = contractType self.contractNumber = contractNumber self.contractName = contractName self.startDate = startDate self.endDate = endDate self.contractValue = contractValue self.contractorName = contractorName self.contractorContact = contractorContact self.contractorPhone = contractorPhone self.contractorEmail = contractorEmail } } @Model //Model for contract types final class ContractType{ var contracts: [Contract] @Attribute(.unique)var typeName: String @Attribute(.unique)var typeCode: String var typeDescription: String init(contracts: [Contract], typeName: String = "", typeCode: String = "", typeDescription: String = "") { self.contracts = contracts self.typeName = typeName self.typeCode = typeCode self.typeDescription = typeDescription } } The swiftui code for user entry: ```import SwiftUI import SwiftData struct EnterContractTypes: View { @Environment(\.modelContext) var managedObjectContext @Query private var contracts: [Contract] @Query private var contractTypes: [ContractType] @State private var typeName: String = "" @State private var typeCode: String = "" @State private var typeDescription: String = "" var body: some View { Form { Section(header: Text("Enter Contract Type") .foregroundStyle(Color(.green)) .bold() .font(.largeTitle)) { TextField("Name", text: $typeName) .frame(width: 400, height: 40) TextField("Code", text: $typeCode) .frame(width: 400, height: 40) TextField("Description", text: $typeDescription, axis: .vertical) .frame(width: 600, height: 60) } } .frame(width:1000, height:500) } } Last post used code block but appears to have failed. I did not check it because I was in a hurry, sorry.
Sep ’25
Reply to Cannot Export Mac app from Xcode 26.4
Oddly Xcode fixed itself
Replies
Boosts
Views
Activity
2w
Reply to Cannot Export Mac app from Xcode 26.4
Discovered a work around to allow me to get to the app and move it to the applications folder.
Replies
Boosts
Views
Activity
2w
Reply to Cannot Export Mac app from Xcode 26.4
OOPS 26.3 hit wrong key sorry.
Replies
Boosts
Views
Activity
2w
Reply to Swiftui Picker with optional value selected in picker
There were two issues. First there had to be a default for the nil case and second the pick could not be an array. That fixed the problem
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Oct ’25
Reply to Picker using SwiftData
The model appears to have not pasted correctly, I used the code block. So here it is again. import SwiftData //Model one: type of contract, i.e. Firm Fixed Price, etc @Model final class TypeOfContract { var contracts: [Contract] @Attribute(.unique) var typeName: String @Attribute(.unique) var typeCode: String var typeDescription: String init(contracts: [Contract], typeName: String = "", typeCode: String = "", typeDescription: String = "") { self.contracts = contracts self.typeName = typeName self.typeCode = typeCode self.typeDescription = typeDescription } } //Model two: the Contract @Model final class Contract { var contractType: TypeOfContract? var costReports: [CostReport] @Attribute(.unique) var contractNumber: String @Attribute(.unique) var contractName: String var startDate: Date var endDate: Date var contractValue: Decimal var contractCompany: String var contractContact: String var contactEmail: String var contactPhone: String var contractNotes: String init(contractType: TypeOfContract? = nil, costReports: [CostReport], contractNumber: String = "", contractName: String = "", startDate: Date = .now, endDate: Date = .now, contractValue: Decimal = 0.00, contractCompany: String = "", contractContact: String = "", contactEmail: String = "", contactPhone: String = "", contractNotes: String = "") { self.contractType = contractType self.costReports = costReports self.contractNumber = contractNumber self.contractName = contractName self.startDate = startDate self.endDate = endDate self.contractValue = contractValue self.contractCompany = contractCompany self.contractContact = contractContact self.contactEmail = contactEmail self.contactPhone = contactPhone self.contractNotes = contractNotes } } //Model Three: The Cost Reports @Model final class CostReport { var contract: Contract? var periodStartDate: Date var periodEndDate: Date var bCWP: Double //Budgeted Cost Work Performed var aCWP: Double //Actual Cost Work Performed var bCWS: Double //Budgeted Cost Work Scheduled //Calculated fields init(contract: Contract? = nil, periodStartDate: Date = .now, periodEndDate: Date = .now, bCWP: Double = 0.0, aCWP: Double = 0.0, bCWS: Double = 0.0) { self.contract = contract self.periodStartDate = periodStartDate self.periodEndDate = periodEndDate self.bCWP = bCWP self.aCWP = aCWP self.bCWS = bCWS } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Oct ’25
Reply to Present User an error message when SwiftData save fails
Do you know of any examples of such code?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Oct ’25
Reply to Present User an error message when SwiftData save fails
How do I prevent an upsert?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Oct ’25
Reply to Present User an error message when SwiftData save fails
As a note, I removed the unique modifiers on the two fields because I discovered another issue. When I was saving a duplicate field value, it was not failing, it was replacing the current with a new record, thus it remained unique because the old record was replaced.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Oct ’25
Reply to Present User an error message when SwiftData save fails
Here is the model
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Oct ’25
Reply to Present User an error message when SwiftData save fails
import SwiftData //Model one: type of contract, i.e. Firm Fixed Price, etc @Model final class TypeOfContract { var contracts: [Contract] var typeName: String var typeCode: String var typeDescription: String init(contracts: [Contract], typeName: String = "", typeCode: String = "", typeDescription: String = "") { self.contracts = contracts self.typeName = typeName self.typeCode = typeCode self.typeDescription = typeDescription } } //Model two: the Contract @Model final class Contract { var contractType: TypeOfContract? var costReports: [CostReport] var contractNumber: String var contractName: String var startDate: Date var endDate: Date var contractValue: Double var contractCompany: String var contractContact: String var contactEmail: String var contactPhone: String var contractNotes: String init(contractType: TypeOfContract? = nil, costReports: [CostReport], contractNumber: String = "", contractName: String = "", startDate: Date = .now, endDate: Date = .now, contractValue: Double = 0.0, contractCompany: String = "", contractContact: String = "", contactEmail: String = "", contactPhone: String = "", contractNotes: String = "") { self.contractType = contractType self.costReports = costReports self.contractNumber = contractNumber self.contractName = contractName self.startDate = startDate self.endDate = endDate self.contractValue = contractValue self.contractCompany = contractCompany self.contractContact = contractContact self.contactEmail = contactEmail self.contactPhone = contactPhone self.contractNotes = contractNotes } } //Model Three: The Cost Reports @Model final class CostReport { var contract: Contract? var periodStartDate: Date var periodEndDate: Date var bCWP: Double //Budgeted Cost Work Performed var aCWP: Double //Actual Cost Work Performed var bCWS: Double //Budgeted Cost Work Scheduled //Calculated fields init(contract: Contract? = nil, periodStartDate: Date = .now, periodEndDate: Date = .now, bCWP: Double = 0.0, aCWP: Double = 0.0, bCWS: Double = 0.0) { self.contract = contract self.periodStartDate = periodStartDate self.periodEndDate = periodEndDate self.bCWP = bCWP self.aCWP = aCWP self.bCWS = bCWS } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Oct ’25
Reply to Save SwiftData object for model with one to many relationship
Found the issue: In a one to many relationship to be able to enter the one side of the relationship you need to identify the many side, even if you are not entering any values for the many side. In this case the many side is Contracts and a state variable needs to be declared @State private var contracts: [Contract] = [] with an empty array.
Replies
Boosts
Views
Activity
Sep ’25
Reply to Save SwiftData object for model with one to many relationship
OOPS! forgot the save function let contractType = ContractType() contractType.typeName = typeName contractType.typeCode = typeCode contractType.typeDescription = typeDescription modelContext.insert(contractType) try? modelContext.save() } }
Replies
Boosts
Views
Activity
Sep ’25
Reply to Save SwiftData object for model with one to many relationship
import SwiftData //Model for Earned Value Analysis The Model @Model final class CostReport{ var aCWP: Double //Actual Cost of Work Performed var bCWP: Double //Budgeted Cost of Work Performed var bCWS: Double // Budgeted Cost of Work Scheduled var cumACWP: Double// Cumlative Actual Cost of Work Performed var cumBCWP: Double // Cumlative Budgeted Cost of Work Performed var cumBCWS: Double // Cumlative Budgeted Cost of Work Scheduled var startDateOfPeriod: Date var endDateOfPeriod: Date var contract: Contract? init(aCWP: Double = 0.0, bCWP: Double = 0.0, bCWS: Double = 0.0, cumACWP: Double = 0.0, cumBCWP: Double = 0.0, cumBCWS: Double = 0.0, startDateOfPeriod: Date = .now, endDateOfPeriod: Date = .now, contract: Contract) { self.aCWP = aCWP self.bCWP = bCWP self.bCWS = bCWS self.cumACWP = cumACWP self.cumBCWP = cumBCWP self.cumBCWS = cumBCWS self.startDateOfPeriod = startDateOfPeriod self.endDateOfPeriod = endDateOfPeriod self.contract = contract } } @Model //Model for Contracts final class Contract{ var costReports: [CostReport] var contractType: ContractType? @Attribute(.unique)var contractNumber: String @Attribute(.unique)var contractName: String var startDate: Date var endDate: Date var contractValue: Double var contractorName: String var contractorContact: String var contractorPhone: String var contractorEmail: String init(costReports: [CostReport], contractType: ContractType, contractNumber: String = "", contractName: String = "", startDate: Date = .now, endDate: Date = .now, contractValue: Double = 0.0, contractorName: String = "", contractorContact: String = "", contractorPhone: String = "", contractorEmail: String = "") { self.costReports = costReports self.contractType = contractType self.contractNumber = contractNumber self.contractName = contractName self.startDate = startDate self.endDate = endDate self.contractValue = contractValue self.contractorName = contractorName self.contractorContact = contractorContact self.contractorPhone = contractorPhone self.contractorEmail = contractorEmail } } @Model //Model for contract types final class ContractType{ var contracts: [Contract] @Attribute(.unique)var typeName: String @Attribute(.unique)var typeCode: String var typeDescription: String init(contracts: [Contract], typeName: String = "", typeCode: String = "", typeDescription: String = "") { self.contracts = contracts self.typeName = typeName self.typeCode = typeCode self.typeDescription = typeDescription } } The swiftui code for user entry: ```import SwiftUI import SwiftData struct EnterContractTypes: View { @Environment(\.modelContext) var managedObjectContext @Query private var contracts: [Contract] @Query private var contractTypes: [ContractType] @State private var typeName: String = "" @State private var typeCode: String = "" @State private var typeDescription: String = "" var body: some View { Form { Section(header: Text("Enter Contract Type") .foregroundStyle(Color(.green)) .bold() .font(.largeTitle)) { TextField("Name", text: $typeName) .frame(width: 400, height: 40) TextField("Code", text: $typeCode) .frame(width: 400, height: 40) TextField("Description", text: $typeDescription, axis: .vertical) .frame(width: 600, height: 60) } } .frame(width:1000, height:500) } } Last post used code block but appears to have failed. I did not check it because I was in a hurry, sorry.
Replies
Boosts
Views
Activity
Sep ’25
Reply to Copy file in application document file to user Documents file
Based on your suggestion I found a simple way to accomplish the task. I feed the url for the pdf that is created to Preview which displays the file. From here the user can save the file or print it. Thanks for your help
Topic: App & System Services SubTopic: Core OS Tags:
Replies
Boosts
Views
Activity
Aug ’25