Post

Replies

Boosts

Views

Activity

Reply to Failed to product diagnostic error
I think the main error comes from a missing NavigationStack in RosaView. And you should have rosa In addition, I had to test on an iPad in order for Table to show columns names and all columns (see here: https://stackoverflow.com/questions/75277014/swift-table-only-show-1-column) Finally, I made a few changes before it works ok. Please test and tell if that works for you, otherwise, explain what is failing. struct Rosa: Identifiable, Codable, Hashable, Equatable { var id = UUID() let stagione: String let nomeGiocatore: String let cognomeGiocatore: String let nascitaGiocatore: String let etàGiocatore: Int let ruoloGiocatore: String init(stagione: String, nomeGiocatore: String, cognomeGiocatore: String, nascitaGiocatore: String, etàGiocatore: Int, ruoloGiocatore: String) { self.stagione = stagione self.nomeGiocatore = nomeGiocatore self.cognomeGiocatore = cognomeGiocatore self.nascitaGiocatore = nascitaGiocatore self.etàGiocatore = etàGiocatore self.ruoloGiocatore = ruoloGiocatore let dateFormatter = DateFormatter() dateFormatter.dateFormat = "dd-MM-yyyy" guard let birthDate = dateFormatter.date(from: nascitaGiocatore) else { return} let currentYear = Calendar.current.component(.year, from: Date()) let birthYear = Calendar.current.component(.year, from: birthDate) _ = currentYear - birthYear } static func testRosa() -> [Rosa] { return [ Rosa(stagione: "2023/2024", nomeGiocatore: "Matt", cognomeGiocatore: "Bar", nascitaGiocatore: "31-03-2000", etàGiocatore: 24, ruoloGiocatore: "Portiere"), Rosa(stagione: "2023/2024", nomeGiocatore: "Fabio", cognomeGiocatore: "Bel", nascitaGiocatore: "30-11-1982", etàGiocatore: 41, ruoloGiocatore: "Difensore"), Rosa(stagione: "2023/2024", nomeGiocatore: "Ale", cognomeGiocatore: "Nev", nascitaGiocatore: "23-04-2001", etàGiocatore: 23, ruoloGiocatore: "Attaccante"), Rosa(stagione: "2022/2023", nomeGiocatore: "Matte", cognomeGiocatore: "Repe", nascitaGiocatore: "28-02-1999", etàGiocatore: 25, ruoloGiocatore: "Centrocampista") ] } } // Run on iPad: See https://stackoverflow.com/questions/75277014/swift-table-only-show-1-column struct RosaView: View { @State var rosa: [Rosa] = Rosa.testRosa() @State private var apriNuovoGiocatore = false @State var stagione: String = "2023/2024" var rosaFiltrata: [Rosa] { rosa.filter { // <<-- replace Rosa.testRosa() $0.stagione == stagione } } @State private var selezioneGiocatore: Set<Rosa.ID> = [] @State private var ordine = [KeyPathComparator(\Rosa.ruoloGiocatore)] var body: some View { NavigationStack { // <<-- ADD THIS VStack(alignment: .leading) { Text("Stagione: \(stagione)") .fontWeight(.bold) .font(.headline) .foregroundColor(.blue) .padding() Table(rosaFiltrata, selection: $selezioneGiocatore, sortOrder: $ordine) { TableColumn("Nome", value: \.nomeGiocatore) // REMOVE Text( and .foregroundStyle(.blue) TableColumn("Cognome", value: \.cognomeGiocatore) TableColumn("Ruolo", value: \.ruoloGiocatore) TableColumn("Data di nascita", value: \.nascitaGiocatore) TableColumn("Età") { rosa in Text("\(rosa.etàGiocatore)") } .width(100) } } .frame(width: 700, height: 300) .toolbar { Button { apriNuovoGiocatore = true } label: { Image(systemName: "person.badge.plus") .foregroundColor(.blue) } .sheet(isPresented: $apriNuovoGiocatore, content: { NuovoGiocatore(rosaArray: $rosa, nomeNuovoGiocatore: "", cognomeNuovoGiocatore: "", nascitaNuovoGiocatore: "", ruoloNuovoGiocatore: "", etàNuovoGiocatore: 20) // <<-- pass $rosa as parameter to a Binding }) } .navigationTitle("Rosa") } } } struct NuovoGiocatore: View { // should start with uppercase @Environment(\.dismiss) var dismiss @Binding var rosaArray : [Rosa] // <<-- ADD THIS @State var nomeNuovoGiocatore: String = "" @State var cognomeNuovoGiocatore: String = "" @State var nascitaNuovoGiocatore: String = "" @State var ruoloNuovoGiocatore: String = "" @State var etàNuovoGiocatore: Int = 20 var body: some View { NavigationStack { Form { TextField("Nome:", text: $nomeNuovoGiocatore) TextField("Cognome:", text: $cognomeNuovoGiocatore) } .navigationTitle("Nuovo giocatore") .toolbar { Button("Cancel") { dismiss() } Button("Aggiungi giocatore") { let nuovoGiocatore = Rosa(stagione: "2023/2024", nomeGiocatore: nomeNuovoGiocatore, cognomeGiocatore: cognomeNuovoGiocatore, nascitaGiocatore: nascitaNuovoGiocatore, etàGiocatore: etàNuovoGiocatore, ruoloGiocatore: ruoloNuovoGiocatore) // REMOVE THIS var rosa = Rosa.testRosa() rosaArray.append(nuovoGiocatore) dismiss() } } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’24
Reply to I plan to study iOS Development. I need to buy a MacBook.
For Xcode development, the most important, in order: recent enough Mac (in case you buy a second hand), so 2020 as the oldest to be able to load recent OS version and consequently Xcode If you intend to develop for visionOS, you need a M-Mac. And M-Mac is future proof. Disk storage: 500 MB is a minimum, more (1 TB or even 2 TB) is just better. Xcode is disk hungry for installing Memory: 8 GB may soon be short, specially because you will not be able to upgrade. I would advise 16 GB Screen size: Xcode needs to display a lot of content. So, a good compromise if you buy a MacBook is to buy an external large display (21" or better 27"). But that could be a second step purchase. Performance: that's not critical (you don't really care if building the app takes 40 seconds instead of 30). Chip is not crucial (M1 quite enough). So, at the end: you can look for a MacBook, possibly second hand, with: any number of Core CPU: 8-Core CPU is OK any number of Core GPU: 8-Core GPU is OK 16GB Unified Memory 512GB SSD Storage or better 1TB 13" screen if you can buy a large (21") external display later. Otherwise, 15" is much preferred. M-Mac. M1 chip would still be OK.
Topic: Programming Languages SubTopic: Swift Tags:
Mar ’24
Reply to New App for my Business
Welcome to the forum. First, you must be sure that your app will provide more functionality than a website. Otherwise it may be rejected from the appstore. And it would not be worth the effort. -> See appstore guidelines 4.2 Minimum Functionality https://developer.apple.com/app-store/review/guidelines/#design For the stack, you should probably use Xcode and SwiftUI (you could use UIKit but I understand your app would be well suited for SwiftUI development). Budgeting: do you intend to develop yourself ? Then budget is mainly your training time to learn to develop. If you subcontract, the general rules for subcontracting a software development will apply: cost will be very variable from one developer to another make sure you keep all the rights and submit the app yourself to the appstore, not by the developer and gat all source code, documented properly, to be able to continue if you decide to change to a different developer.
Mar ’24
Reply to Screenshot for iOS and iPad
My iPad screen shot is using the same version as iPhone That's a mistake. You have to provide screenshots as required by AppStoreConnect. Required are: iPhone Screen 6.5" (e.g., iPhone 11 Pro Max) iPhone Screen 5.5" (e.g, iPhone 8 Plus) iPad Pro (6th gen) Screen 12.9" iPad Pro (2nd gen) Screen 12.9"   iPhone Screen 6.7" (e.g., iPhone 15 Pro Max) is optional. So that means at least 4 sets of screenshots from simulators.
Mar ’24
Reply to Failed to product diagnostic error
Yes, you are doing something (in fact several things) wrong. But to tell you what, you should show the complete Rosa structure. However, some problems: @State private var selezioneGiocatore: Rosa.ID? = nil Selection must be a Set, as @State private var selezioneGiocatore: Set<Rosa.ID> = [] TableColumn(Text("Età").foregroundStyle(.blue)) { Rosa in Text("\(Rosa.etàGiocatore)") } Using the structure name (Rosa) as the argument of the closure is incorrect. Rosa.testRosa().append(nuovoGiocatore) How is testRosa defined ? Seems to be a func ? append is trying to modify the structure, which is forbidden. You should call it on an instance as: var rosa = Rosa.testRosa() rosa.append(nuovoGiocatore) So please show complete code.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’24
Reply to 关于隐私清单
I would like to know for users who have not updated the app after May 1, will they be affected? 我想知道对于5月1日之后,用户如果没有更新app,是否会受到影响? 用户不应受到影响。只有开发人员在提交新版本时才需要修改应用程序。 Users should not be affected. Only developers have to modify the app when they submit a new version.
Mar ’24
Reply to Delay Issue with onChange Modifier in SwiftUI
Explanation is simple. Error comes from the fact that modelIndex used in selection may not be valid because there is no brand selected or because for the selected brand models count is lower than the current selection. And, as you noticed, the view is redrawn before on change is completed. By moving the if brandIndex != 0 test upward, you avoid the error when you select 'no brand'. Then, you don't see the picker but the Text asking to select a brand. But that was not enough. For instance, if you selected Renault Zoe (modelIndex = 3) and then select Porsche (only 2 models), view is updated and fails because modelIndex is still 3 (onChange not yet executed). With the change, if brandIndex != 0 && modelIndex <= cars[brandIndex - 1].models.count { // <<-- test modelIndex Picker("Model", selection: $modelIndex) { Text("Model Seçin").tag(0) // if brandIndex != 0 { let _ = print("modelIndex", modelIndex) // I'm getting first 3 then 0. And I'm getting error. ForEach(cars[brandIndex - 1].models.indices, id: \.self) { Text(cars[brandIndex - 1].models[$0]) .tag($0 + 1) } } } else { Text("Select a brand") } and not with: } else { // This section for design Picker("Model", selection: $modelIndex) { Text("Seçin").tag(0) } } then when you select Porsche, count is not OK, so you display Text("Select a brand") but you have no time to see, as onChange fires and then you see the Picker selection. To have time to see it, change the onChange as follows: .onChange(of: brandIndex) { print("changing brand", brandIndex) DispatchQueue.main.asyncAfter(deadline: .now() + 1) { // <<-- To get time to see the Text displayed if modelIndex != 0 { modelIndex = 0 } } } If that works, don't forget to close the thread on the correct answer. Otherwise explain the use case and the error.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’24
Reply to Delay Issue with onChange Modifier in SwiftUI
@ FiratSirin You're right, you need an additional test: if brandIndex != 0 && modelIndex <= cars[brandIndex - 1].models.count { // <<-- test modelIndex Picker("Model", selection: $modelIndex) { Text("Model Seçin").tag(0) // if brandIndex != 0 { let _ = print("modelIndex", modelIndex) // I'm getting first 3 then 0. And I'm getting error. ForEach(cars[brandIndex - 1].models.indices, id: \.self) { Text(cars[brandIndex - 1].models[$0]) .tag($0 + 1) } } } else { Text("Select a brand") } Here is the complete code I tested: struct Car: Decodable, Identifiable { enum CodingKeys: CodingKey { case brand case models } var id = UUID() var brand: String var models: [String] } struct ContentView: View { @State var brandIndex = 0 @State var modelIndex = 0 var cars : [Car] = [ Car(brand: "VW", models: ["Golf", "Passat", "Up"]), Car(brand: "Renault", models: ["Megane", "Twingo", "Zoe"]), Car(brand: "Porsche", models: ["Carrera", "Taycan"]) ] var body: some View { Picker("Marka", selection: $brandIndex) { Text("Marka Seçin").tag(0) ForEach(cars.indices, id: \.self) { Text(cars[$0].brand).tag($0 + 1) } } .onChange(of: brandIndex) { print("changing brand", brandIndex) if modelIndex != 0 { modelIndex = 0 } } if brandIndex != 0 && modelIndex <= cars[brandIndex - 1].models.count { Picker("Model", selection: $modelIndex) { Text("Model Seçin").tag(0) // if brandIndex != 0 { let _ = print("modelIndex", modelIndex) // I'm getting first 3 then 0. And I'm getting error. ForEach(cars[brandIndex - 1].models.indices, id: \.self) { Text(cars[brandIndex - 1].models[$0]) .tag($0 + 1) } } } else { Text("Select a brand") } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’24
Reply to Delay Issue with onChange Modifier in SwiftUI
Just move the if brandIndex != 0 { test if brandIndex != 0 { Picker("Model", selection: $modelIndex) { Text("Model Seçin").tag(0) // if brandIndex != 0 { let _ = print("modelIndex", modelIndex) // I'm getting first 3 then 0. And I'm getting error. ForEach(cars[brandIndex - 1].models.indices, id: \.self) { Text(cars[brandIndex - 1].models[$0]) .tag($0 + 1) } } } else { Text("Select a brand") }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’24
Reply to Access an Object and Change Values. up to 2 Class Levels down
Here is an example if I understand correctly what you want to achieve. I have changed some var names to make it clearer for me when coding. I also removed the @Model for this test. /*@Model*/ class Father { var name: String = "" var child: Child? } /*@Model*/ class Child { var name: String = "" var grandChild: GrandChild? var father: Father = Father() } /*@Model*/ class GrandChild { var name: String = "" var parent: Child = Child() } struct ContentView: View { @State var fatherEd = Father() @State var childBob = Child() @State var grandChildTom = GrandChild() @State var someChange = false // a trick to force a state change and force redraw ; could be replaced by Observed, but simpler here init() { grandChildTom.name = "Tom" grandChildTom.parent = childBob childBob.name = "Bob" childBob.grandChild = grandChildTom childBob.father = fatherEd fatherEd.name = "father Eduard" fatherEd.child = childBob } var body: some View { VStack { Text("Hello") Spacer() .frame(height: 20) if someChange || !someChange { Text("father:\(fatherEd.name)") Spacer() .frame(height: 20) if fatherEd.child != nil { Text("child:\(childBob.name)") Text("same as \(fatherEd.child!.name)") } Spacer() .frame(height: 20) if fatherEd.child?.grandChild != nil { Text("grandchild:\(grandChildTom.name)") Text("same as \(fatherEd.child!.grandChild!.name)") } Spacer() .frame(height: 20) } Button(action: { // Change the grandChild name through the child if someChange { fatherEd.child?.grandChild?.name = "Tom" } else { fatherEd.child?.grandChild?.name = "Laureen" } someChange.toggle() // just to force a change in the state var. }) { if someChange { Text("Revert grandChild name to Tom") } else { Text("Change grandChild name to Laureen") } } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’24
Reply to Failed to product diagnostic error
I think the main error comes from a missing NavigationStack in RosaView. And you should have rosa In addition, I had to test on an iPad in order for Table to show columns names and all columns (see here: https://stackoverflow.com/questions/75277014/swift-table-only-show-1-column) Finally, I made a few changes before it works ok. Please test and tell if that works for you, otherwise, explain what is failing. struct Rosa: Identifiable, Codable, Hashable, Equatable { var id = UUID() let stagione: String let nomeGiocatore: String let cognomeGiocatore: String let nascitaGiocatore: String let etàGiocatore: Int let ruoloGiocatore: String init(stagione: String, nomeGiocatore: String, cognomeGiocatore: String, nascitaGiocatore: String, etàGiocatore: Int, ruoloGiocatore: String) { self.stagione = stagione self.nomeGiocatore = nomeGiocatore self.cognomeGiocatore = cognomeGiocatore self.nascitaGiocatore = nascitaGiocatore self.etàGiocatore = etàGiocatore self.ruoloGiocatore = ruoloGiocatore let dateFormatter = DateFormatter() dateFormatter.dateFormat = "dd-MM-yyyy" guard let birthDate = dateFormatter.date(from: nascitaGiocatore) else { return} let currentYear = Calendar.current.component(.year, from: Date()) let birthYear = Calendar.current.component(.year, from: birthDate) _ = currentYear - birthYear } static func testRosa() -> [Rosa] { return [ Rosa(stagione: "2023/2024", nomeGiocatore: "Matt", cognomeGiocatore: "Bar", nascitaGiocatore: "31-03-2000", etàGiocatore: 24, ruoloGiocatore: "Portiere"), Rosa(stagione: "2023/2024", nomeGiocatore: "Fabio", cognomeGiocatore: "Bel", nascitaGiocatore: "30-11-1982", etàGiocatore: 41, ruoloGiocatore: "Difensore"), Rosa(stagione: "2023/2024", nomeGiocatore: "Ale", cognomeGiocatore: "Nev", nascitaGiocatore: "23-04-2001", etàGiocatore: 23, ruoloGiocatore: "Attaccante"), Rosa(stagione: "2022/2023", nomeGiocatore: "Matte", cognomeGiocatore: "Repe", nascitaGiocatore: "28-02-1999", etàGiocatore: 25, ruoloGiocatore: "Centrocampista") ] } } // Run on iPad: See https://stackoverflow.com/questions/75277014/swift-table-only-show-1-column struct RosaView: View { @State var rosa: [Rosa] = Rosa.testRosa() @State private var apriNuovoGiocatore = false @State var stagione: String = "2023/2024" var rosaFiltrata: [Rosa] { rosa.filter { // <<-- replace Rosa.testRosa() $0.stagione == stagione } } @State private var selezioneGiocatore: Set<Rosa.ID> = [] @State private var ordine = [KeyPathComparator(\Rosa.ruoloGiocatore)] var body: some View { NavigationStack { // <<-- ADD THIS VStack(alignment: .leading) { Text("Stagione: \(stagione)") .fontWeight(.bold) .font(.headline) .foregroundColor(.blue) .padding() Table(rosaFiltrata, selection: $selezioneGiocatore, sortOrder: $ordine) { TableColumn("Nome", value: \.nomeGiocatore) // REMOVE Text( and .foregroundStyle(.blue) TableColumn("Cognome", value: \.cognomeGiocatore) TableColumn("Ruolo", value: \.ruoloGiocatore) TableColumn("Data di nascita", value: \.nascitaGiocatore) TableColumn("Età") { rosa in Text("\(rosa.etàGiocatore)") } .width(100) } } .frame(width: 700, height: 300) .toolbar { Button { apriNuovoGiocatore = true } label: { Image(systemName: "person.badge.plus") .foregroundColor(.blue) } .sheet(isPresented: $apriNuovoGiocatore, content: { NuovoGiocatore(rosaArray: $rosa, nomeNuovoGiocatore: "", cognomeNuovoGiocatore: "", nascitaNuovoGiocatore: "", ruoloNuovoGiocatore: "", etàNuovoGiocatore: 20) // <<-- pass $rosa as parameter to a Binding }) } .navigationTitle("Rosa") } } } struct NuovoGiocatore: View { // should start with uppercase @Environment(\.dismiss) var dismiss @Binding var rosaArray : [Rosa] // <<-- ADD THIS @State var nomeNuovoGiocatore: String = "" @State var cognomeNuovoGiocatore: String = "" @State var nascitaNuovoGiocatore: String = "" @State var ruoloNuovoGiocatore: String = "" @State var etàNuovoGiocatore: Int = 20 var body: some View { NavigationStack { Form { TextField("Nome:", text: $nomeNuovoGiocatore) TextField("Cognome:", text: $cognomeNuovoGiocatore) } .navigationTitle("Nuovo giocatore") .toolbar { Button("Cancel") { dismiss() } Button("Aggiungi giocatore") { let nuovoGiocatore = Rosa(stagione: "2023/2024", nomeGiocatore: nomeNuovoGiocatore, cognomeGiocatore: cognomeNuovoGiocatore, nascitaGiocatore: nascitaNuovoGiocatore, etàGiocatore: etàNuovoGiocatore, ruoloGiocatore: ruoloNuovoGiocatore) // REMOVE THIS var rosa = Rosa.testRosa() rosaArray.append(nuovoGiocatore) dismiss() } } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’24
Reply to Looking for apple visionOS developer
Welcome to the forum. But please note that the forum is not intended for such sollicitation. Once you get a developer, you will use the forum to ask for technical questions about your project development.
Topic: App & System Services SubTopic: Core OS Tags:
Replies
Boosts
Views
Activity
Apr ’24
Reply to Looking for apple visionOS developer
Welcome to the forum. But please note that the forum is not intended for such sollicitation. Once you get a developer, you will use the forum to ask for technical questions about your project development.
Topic: App & System Services SubTopic: Core OS Tags:
Replies
Boosts
Views
Activity
Mar ’24
Reply to I plan to study iOS Development. I need to buy a MacBook.
For Xcode development, the most important, in order: recent enough Mac (in case you buy a second hand), so 2020 as the oldest to be able to load recent OS version and consequently Xcode If you intend to develop for visionOS, you need a M-Mac. And M-Mac is future proof. Disk storage: 500 MB is a minimum, more (1 TB or even 2 TB) is just better. Xcode is disk hungry for installing Memory: 8 GB may soon be short, specially because you will not be able to upgrade. I would advise 16 GB Screen size: Xcode needs to display a lot of content. So, a good compromise if you buy a MacBook is to buy an external large display (21" or better 27"). But that could be a second step purchase. Performance: that's not critical (you don't really care if building the app takes 40 seconds instead of 30). Chip is not crucial (M1 quite enough). So, at the end: you can look for a MacBook, possibly second hand, with: any number of Core CPU: 8-Core CPU is OK any number of Core GPU: 8-Core GPU is OK 16GB Unified Memory 512GB SSD Storage or better 1TB 13" screen if you can buy a large (21") external display later. Otherwise, 15" is much preferred. M-Mac. M1 chip would still be OK.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Mar ’24
Reply to New App for my Business
Welcome to the forum. First, you must be sure that your app will provide more functionality than a website. Otherwise it may be rejected from the appstore. And it would not be worth the effort. -> See appstore guidelines 4.2 Minimum Functionality https://developer.apple.com/app-store/review/guidelines/#design For the stack, you should probably use Xcode and SwiftUI (you could use UIKit but I understand your app would be well suited for SwiftUI development). Budgeting: do you intend to develop yourself ? Then budget is mainly your training time to learn to develop. If you subcontract, the general rules for subcontracting a software development will apply: cost will be very variable from one developer to another make sure you keep all the rights and submit the app yourself to the appstore, not by the developer and gat all source code, documented properly, to be able to continue if you decide to change to a different developer.
Replies
Boosts
Views
Activity
Mar ’24
Reply to Screenshot for iOS and iPad
My iPad screen shot is using the same version as iPhone That's a mistake. You have to provide screenshots as required by AppStoreConnect. Required are: iPhone Screen 6.5" (e.g., iPhone 11 Pro Max) iPhone Screen 5.5" (e.g, iPhone 8 Plus) iPad Pro (6th gen) Screen 12.9" iPad Pro (2nd gen) Screen 12.9"   iPhone Screen 6.7" (e.g., iPhone 15 Pro Max) is optional. So that means at least 4 sets of screenshots from simulators.
Replies
Boosts
Views
Activity
Mar ’24
Reply to Failed to product diagnostic error
Yes, you are doing something (in fact several things) wrong. But to tell you what, you should show the complete Rosa structure. However, some problems: @State private var selezioneGiocatore: Rosa.ID? = nil Selection must be a Set, as @State private var selezioneGiocatore: Set<Rosa.ID> = [] TableColumn(Text("Età").foregroundStyle(.blue)) { Rosa in Text("\(Rosa.etàGiocatore)") } Using the structure name (Rosa) as the argument of the closure is incorrect. Rosa.testRosa().append(nuovoGiocatore) How is testRosa defined ? Seems to be a func ? append is trying to modify the structure, which is forbidden. You should call it on an instance as: var rosa = Rosa.testRosa() rosa.append(nuovoGiocatore) So please show complete code.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Mar ’24
Reply to 关于隐私清单
I would like to know for users who have not updated the app after May 1, will they be affected? 我想知道对于5月1日之后,用户如果没有更新app,是否会受到影响? 用户不应受到影响。只有开发人员在提交新版本时才需要修改应用程序。 Users should not be affected. Only developers have to modify the app when they submit a new version.
Replies
Boosts
Views
Activity
Mar ’24
Reply to Access an Object and Change Values. up to 2 Class Levels down
I tested the code I posted and it works OK. I could not test your code, missing parts to do so. actually the objects are arrays of objects and I access them through ¢Bindable vars Could you post the code showing what those objects are ?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Mar ’24
Reply to Delay Issue with onChange Modifier in SwiftUI
Explanation is simple. Error comes from the fact that modelIndex used in selection may not be valid because there is no brand selected or because for the selected brand models count is lower than the current selection. And, as you noticed, the view is redrawn before on change is completed. By moving the if brandIndex != 0 test upward, you avoid the error when you select 'no brand'. Then, you don't see the picker but the Text asking to select a brand. But that was not enough. For instance, if you selected Renault Zoe (modelIndex = 3) and then select Porsche (only 2 models), view is updated and fails because modelIndex is still 3 (onChange not yet executed). With the change, if brandIndex != 0 && modelIndex <= cars[brandIndex - 1].models.count { // <<-- test modelIndex Picker("Model", selection: $modelIndex) { Text("Model Seçin").tag(0) // if brandIndex != 0 { let _ = print("modelIndex", modelIndex) // I'm getting first 3 then 0. And I'm getting error. ForEach(cars[brandIndex - 1].models.indices, id: \.self) { Text(cars[brandIndex - 1].models[$0]) .tag($0 + 1) } } } else { Text("Select a brand") } and not with: } else { // This section for design Picker("Model", selection: $modelIndex) { Text("Seçin").tag(0) } } then when you select Porsche, count is not OK, so you display Text("Select a brand") but you have no time to see, as onChange fires and then you see the Picker selection. To have time to see it, change the onChange as follows: .onChange(of: brandIndex) { print("changing brand", brandIndex) DispatchQueue.main.asyncAfter(deadline: .now() + 1) { // <<-- To get time to see the Text displayed if modelIndex != 0 { modelIndex = 0 } } } If that works, don't forget to close the thread on the correct answer. Otherwise explain the use case and the error.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Mar ’24
Reply to Delay Issue with onChange Modifier in SwiftUI
@ FiratSirin You're right, you need an additional test: if brandIndex != 0 && modelIndex <= cars[brandIndex - 1].models.count { // <<-- test modelIndex Picker("Model", selection: $modelIndex) { Text("Model Seçin").tag(0) // if brandIndex != 0 { let _ = print("modelIndex", modelIndex) // I'm getting first 3 then 0. And I'm getting error. ForEach(cars[brandIndex - 1].models.indices, id: \.self) { Text(cars[brandIndex - 1].models[$0]) .tag($0 + 1) } } } else { Text("Select a brand") } Here is the complete code I tested: struct Car: Decodable, Identifiable { enum CodingKeys: CodingKey { case brand case models } var id = UUID() var brand: String var models: [String] } struct ContentView: View { @State var brandIndex = 0 @State var modelIndex = 0 var cars : [Car] = [ Car(brand: "VW", models: ["Golf", "Passat", "Up"]), Car(brand: "Renault", models: ["Megane", "Twingo", "Zoe"]), Car(brand: "Porsche", models: ["Carrera", "Taycan"]) ] var body: some View { Picker("Marka", selection: $brandIndex) { Text("Marka Seçin").tag(0) ForEach(cars.indices, id: \.self) { Text(cars[$0].brand).tag($0 + 1) } } .onChange(of: brandIndex) { print("changing brand", brandIndex) if modelIndex != 0 { modelIndex = 0 } } if brandIndex != 0 && modelIndex <= cars[brandIndex - 1].models.count { Picker("Model", selection: $modelIndex) { Text("Model Seçin").tag(0) // if brandIndex != 0 { let _ = print("modelIndex", modelIndex) // I'm getting first 3 then 0. And I'm getting error. ForEach(cars[brandIndex - 1].models.indices, id: \.self) { Text(cars[brandIndex - 1].models[$0]) .tag($0 + 1) } } } else { Text("Select a brand") } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Mar ’24
Reply to Delay Issue with onChange Modifier in SwiftUI
Just move the if brandIndex != 0 { test if brandIndex != 0 { Picker("Model", selection: $modelIndex) { Text("Model Seçin").tag(0) // if brandIndex != 0 { let _ = print("modelIndex", modelIndex) // I'm getting first 3 then 0. And I'm getting error. ForEach(cars[brandIndex - 1].models.indices, id: \.self) { Text(cars[brandIndex - 1].models[$0]) .tag($0 + 1) } } } else { Text("Select a brand") }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Mar ’24
Reply to Access an Object and Change Values. up to 2 Class Levels down
Here is an example if I understand correctly what you want to achieve. I have changed some var names to make it clearer for me when coding. I also removed the @Model for this test. /*@Model*/ class Father { var name: String = "" var child: Child? } /*@Model*/ class Child { var name: String = "" var grandChild: GrandChild? var father: Father = Father() } /*@Model*/ class GrandChild { var name: String = "" var parent: Child = Child() } struct ContentView: View { @State var fatherEd = Father() @State var childBob = Child() @State var grandChildTom = GrandChild() @State var someChange = false // a trick to force a state change and force redraw ; could be replaced by Observed, but simpler here init() { grandChildTom.name = "Tom" grandChildTom.parent = childBob childBob.name = "Bob" childBob.grandChild = grandChildTom childBob.father = fatherEd fatherEd.name = "father Eduard" fatherEd.child = childBob } var body: some View { VStack { Text("Hello") Spacer() .frame(height: 20) if someChange || !someChange { Text("father:\(fatherEd.name)") Spacer() .frame(height: 20) if fatherEd.child != nil { Text("child:\(childBob.name)") Text("same as \(fatherEd.child!.name)") } Spacer() .frame(height: 20) if fatherEd.child?.grandChild != nil { Text("grandchild:\(grandChildTom.name)") Text("same as \(fatherEd.child!.grandChild!.name)") } Spacer() .frame(height: 20) } Button(action: { // Change the grandChild name through the child if someChange { fatherEd.child?.grandChild?.name = "Tom" } else { fatherEd.child?.grandChild?.name = "Laureen" } someChange.toggle() // just to force a change in the state var. }) { if someChange { Text("Revert grandChild name to Tom") } else { Text("Change grandChild name to Laureen") } } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Mar ’24
Reply to Reorder multiple items in list - SwiftUI
I tested in MacOS (destination is MacOS only), it works perfectly.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Mar ’24