Post

Replies

Boosts

Views

Activity

Reply to Error when calling function to move an element: "Cannot use mutating member on immutable value: 'self' is immutable"
Where exactly is the error ? On timer probably. That's because you declare the function as mutating (as compiler asks for). But you have then to make timer a State variable: struct Pos_View: View { @ObservedObject var element_1 = Element(imageName: "pic_1", name: "", size: CGSize(width: 100, height: 100), position: PositionConstants.pos_1) let Input_Matrix = Input.input_1 // Index to track current row in matrix @State var currentIndex = 0 // Timer to trigger updates @State private var timer: Timer? // <<-- State var var body: some View { VStack { // Display element Image(element_1.imageName) .resizable() .frame(width: element_1.size.width, height: element_1.size.height) .position(element_1.position) // Button to start animation Button("Start Animation") { startAnimation() } } } /*mutating*/ func startAnimation() { // <<-- no mutating currentIndex = 0 // Reset index before starting animation timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) {timer in if self.currentIndex < self.Input_Matrix.count { let isFirstElementOne = self.Input_Matrix[self.currentIndex][0] == 1 let newPosition = isFirstElementOne ? PositionConstants.pos_2 : PositionConstants.pos_1 withAnimation { self.element_1.position = newPosition } self.currentIndex += 1 } else { // Stop the timer when we reach the end of matrix timer.invalidate() } } } }
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’24
Reply to SwiftUI app crashes randomly on iOS 17
I am not sure to understand the logic in onChange. Are you sure you want to test selectedTab .onChange(of: selectedTab) { neco in if selectedTab == 2 { and not neco .onChange(of: selectedTab) { neco in if neco == 2 { self.isChatModalPresented = true selectedTab = previousTab } else { previousTab = selectedTab } } When do you reset self.isChatModalPresented to false ?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’24
Reply to Problem with dialogue
@ Climber1987Muc .onAppear works only onetime. Yes, normal. The modifier name is a bit misleading. It works when the view loads (not when it appears on screen. One trick is to create a dummy State var: @State var dummy = false Then in the body, enclose the part on which you had .onAppear inside: if dummy || !dummy { // Will always be true // code on which .onAppear applied } And in each place where you want to force redraw (like in a Button action or scroll onChange), call dummy.toggle() // just to force a change in the state var
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’24
Reply to SwiftUI view update problem
In fact, same occurs without Binding in MyView2. That's because when a State var changes, SwiftUI reevaluates everything that depends on this State var (and does not reevaluate the full body per se). See details here: https://stackoverflow.com/questions/66635304/how-swiftui-detects-there-are-state-variable-values-passed-to-subviews I posted an example based on your code. May be we'll get even deeper insight into SwiftUI.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’24
Reply to SwiftUI view update problem
That's how SwiftUI works. With the Binding on v1 in MyView1 and MyView2, each time you change the State variable v1 (through Bing in MyView1), that propagates to MyView2 through its own Binding. Note: no need to use wrappedValue. This works: Button(action: { v1 = 10 }, label: { Text("OK") }) as well as if v1 == 0 { Text("v1 = \(v1) v2 = \(v2) ") } else { Text("???") }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’24
Reply to It doesn't work with the code to bind the buttons to the following Views.
Problem was mainly how ForEach and NavigationLink where intertwined : https://stackoverflow.com/questions/66017531/swiftui-navigationlink-bug-swiftui-encountered-an-issue-when-pushing-anavigatio/67626758#67626758 Here is a code that works (even though some error messages I had no time to clear): struct Item: Identifiable, Hashable { var id = UUID() var name: String var color: Color var image: String } struct ContentView: View { @State var navigatedItem: String? @State var isActivated = false @ViewBuilder private func selectedView(name: String) -> some View { switch name { case "Medical": MedicalView() case "Illness": IllnessView() case "Vaccune": VaccuneView() case "Dewor": DeworView() case "Allergie": AllergieView() default: EmptyView() } } var body: some View { NavigationView { ZStack(){ Color(.systemGray6).ignoresSafeArea() VStack() { ScrollView(.horizontal, showsIndicators: false) { HStack(spacing: 5) { ForEach(MockData.items, id:\.self) { item in ZStack { Button(action:{ navigatedItem = item.name isActivated = true print(item.name) }) { RoundedRectangle(cornerRadius: 10) .foregroundStyle(item.color.self) .frame(width: 70, height: 70) } Image(systemName: item.image) .foregroundColor(.white) .font(.system(size: 30)) .padding(20) } } .background(){ NavigationLink(destination: selectedView(name: navigatedItem ?? "Medical"), isActive: $isActivated) { EmptyView() } } } //HStack //opciones } // cierre scrollview .padding(.top, 20) .padding(.leading) Spacer() } //cierre Vstack } //cierre de Zstack .navigationTitle("Caracteristicas") .toolbar{ ToolbarItem(placement: .navigationBarLeading) { Button(action:{}, label: { Image(systemName: "switch.2")}) } ToolbarItem(placement: .navigationBarTrailing) { Button(action:{}, label: {Image(systemName: "person.circle")}) } }//toolBar .accentColor(.red) } } } struct MockData { static var items = [ Item3(name: "Medical" ,color: .red, image:"heart"), Item3(name: "Illness" ,color: .blue, image:"pill"), // Illness Item3( name:"Vaccune" ,color: .orange, image: "syringe"), Item3(name: "Dewor" ,color: .green, image: "microbe"), Item3(name: "Allergie" ,color:.purple, image: "allergens")] } struct MedicalView: View { var body: some View { Text("Medical") } } struct IllnessView: View { var body: some View { Text("Illness") } } struct VaccuneView: View { var body: some View { Text("Vaccune") } } struct DeworView: View { var body: some View { Text("Dewor") } } struct AllergieView: View { var body: some View { Text("Allergie") } }
Topic: App & System Services SubTopic: Core OS Tags:
Apr ’24
Reply to Arranging Controller, Scenes & Views Structure
At first I thought, it would be a good idea to arrange multiple scenes, all controlled by the GameViewController, due to not having to duplicate recurring methods or so. But as I thought of, I saw a GameViewController file bigger and bigger and I had the fear to more and more loosing the focus the more scenes I added. It depends how similar are the functions in your scenes. But you can declare the function at the top level (outside any class) and call them from Views. Doing so, GameController will just segue to the appropriate View. Or you could put the fund in a 'Utils' class (maybe as static function) and call Utils.theFunc (if static) or create a Utils instance (utils: Utils) in each Scene (which will allow to adapt some properties) and call utils.myFunc. Hard to say more without knowing more on your code.
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’24
Reply to Error when calling function to move an element: "Cannot use mutating member on immutable value: 'self' is immutable"
Where exactly is the error ? On timer probably. That's because you declare the function as mutating (as compiler asks for). But you have then to make timer a State variable: struct Pos_View: View { @ObservedObject var element_1 = Element(imageName: "pic_1", name: "", size: CGSize(width: 100, height: 100), position: PositionConstants.pos_1) let Input_Matrix = Input.input_1 // Index to track current row in matrix @State var currentIndex = 0 // Timer to trigger updates @State private var timer: Timer? // <<-- State var var body: some View { VStack { // Display element Image(element_1.imageName) .resizable() .frame(width: element_1.size.width, height: element_1.size.height) .position(element_1.position) // Button to start animation Button("Start Animation") { startAnimation() } } } /*mutating*/ func startAnimation() { // <<-- no mutating currentIndex = 0 // Reset index before starting animation timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) {timer in if self.currentIndex < self.Input_Matrix.count { let isFirstElementOne = self.Input_Matrix[self.currentIndex][0] == 1 let newPosition = isFirstElementOne ? PositionConstants.pos_2 : PositionConstants.pos_1 withAnimation { self.element_1.position = newPosition } self.currentIndex += 1 } else { // Stop the timer when we reach the end of matrix timer.invalidate() } } } }
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Apr ’24
Reply to SwiftData fetch array only when i needed
You could have a var (tempVar) in the Query (non State var). Keep the State var in the List. Only update the State var from tempVar in Button action.
Topic: App & System Services SubTopic: iCloud Tags:
Replies
Boosts
Views
Activity
Apr ’24
Reply to SwiftUI app crashes randomly on iOS 17
I am not sure to understand the logic in onChange. Are you sure you want to test selectedTab .onChange(of: selectedTab) { neco in if selectedTab == 2 { and not neco .onChange(of: selectedTab) { neco in if neco == 2 { self.isChatModalPresented = true selectedTab = previousTab } else { previousTab = selectedTab } } When do you reset self.isChatModalPresented to false ?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’24
Reply to Typing issue with captures from Regex Builder
Could you provide complete example so we can reproduce ?
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Apr ’24
Reply to just asking a question for those that are programers and developers
@Macri007 You may be interested to read this about emulators: https://www.macrumors.com/2024/04/14/game-boy-emulator-in-app-store/
Replies
Boosts
Views
Activity
Apr ’24
Reply to UserDefaults value is sometimes returning incorrect value
Are you sure you save UserDefaults in all cases after this : if !UserDefaults.standard.alreadyShownToUser { showUI() UserDefaults.standard.alreadyShownToUser = true } If user quits immediately after this, it may not be saved. And next time app is opened, UserDefaults.standard.alreadyShownToUser is still false.
Topic: App & System Services SubTopic: General Tags:
Replies
Boosts
Views
Activity
Apr ’24
Reply to Problem with dialogue
@ Climber1987Muc .onAppear works only onetime. Yes, normal. The modifier name is a bit misleading. It works when the view loads (not when it appears on screen. One trick is to create a dummy State var: @State var dummy = false Then in the body, enclose the part on which you had .onAppear inside: if dummy || !dummy { // Will always be true // code on which .onAppear applied } And in each place where you want to force redraw (like in a Button action or scroll onChange), call dummy.toggle() // just to force a change in the state var
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Apr ’24
Reply to SwiftUI view update problem
In fact, same occurs without Binding in MyView2. That's because when a State var changes, SwiftUI reevaluates everything that depends on this State var (and does not reevaluate the full body per se). See details here: https://stackoverflow.com/questions/66635304/how-swiftui-detects-there-are-state-variable-values-passed-to-subviews I posted an example based on your code. May be we'll get even deeper insight into SwiftUI.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’24
Reply to SwiftUI view update problem
That's how SwiftUI works. With the Binding on v1 in MyView1 and MyView2, each time you change the State variable v1 (through Bing in MyView1), that propagates to MyView2 through its own Binding. Note: no need to use wrappedValue. This works: Button(action: { v1 = 10 }, label: { Text("OK") }) as well as if v1 == 0 { Text("v1 = \(v1) v2 = \(v2) ") } else { Text("???") }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’24
Reply to just asking a question for those that are programers and developers
If your question is related to developing with Xcode, it is not possible to develop for Xbox. There are Xbox development environments but different from Xcode.
Replies
Boosts
Views
Activity
Apr ’24
Reply to function with automatically increasing percentage (watchOS)
@pabra99 that's not a reproductible example. That's essential, to analyse what occurs. If I had to guess, I would say somewhere right there's the mistake or something's missing: Debugging is not about guessing, but about getting evidence.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Apr ’24
Reply to I fell into hell called 'Asset validation failed ..(ID:**)(10)'
I hope this answer from @eskimo will help. https://forums.developer.apple.com/forums/thread/742981
Replies
Boosts
Views
Activity
Apr ’24
Reply to Resolved text not drawing on canvas (Intel Mac only) if longer than one character
I tested on MacBook Pro (15", 2016) 2,7 GHz Intel Core i7 4 core, MacOS 12.7.1 (21G920) With Xcode Version 14.2 (14C18). It works in simulator iPhone 14 Pro iOS 16.2. Also tested on Mac itself and it works:
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’24
Reply to It doesn't work with the code to bind the buttons to the following Views.
Problem was mainly how ForEach and NavigationLink where intertwined : https://stackoverflow.com/questions/66017531/swiftui-navigationlink-bug-swiftui-encountered-an-issue-when-pushing-anavigatio/67626758#67626758 Here is a code that works (even though some error messages I had no time to clear): struct Item: Identifiable, Hashable { var id = UUID() var name: String var color: Color var image: String } struct ContentView: View { @State var navigatedItem: String? @State var isActivated = false @ViewBuilder private func selectedView(name: String) -> some View { switch name { case "Medical": MedicalView() case "Illness": IllnessView() case "Vaccune": VaccuneView() case "Dewor": DeworView() case "Allergie": AllergieView() default: EmptyView() } } var body: some View { NavigationView { ZStack(){ Color(.systemGray6).ignoresSafeArea() VStack() { ScrollView(.horizontal, showsIndicators: false) { HStack(spacing: 5) { ForEach(MockData.items, id:\.self) { item in ZStack { Button(action:{ navigatedItem = item.name isActivated = true print(item.name) }) { RoundedRectangle(cornerRadius: 10) .foregroundStyle(item.color.self) .frame(width: 70, height: 70) } Image(systemName: item.image) .foregroundColor(.white) .font(.system(size: 30)) .padding(20) } } .background(){ NavigationLink(destination: selectedView(name: navigatedItem ?? "Medical"), isActive: $isActivated) { EmptyView() } } } //HStack //opciones } // cierre scrollview .padding(.top, 20) .padding(.leading) Spacer() } //cierre Vstack } //cierre de Zstack .navigationTitle("Caracteristicas") .toolbar{ ToolbarItem(placement: .navigationBarLeading) { Button(action:{}, label: { Image(systemName: "switch.2")}) } ToolbarItem(placement: .navigationBarTrailing) { Button(action:{}, label: {Image(systemName: "person.circle")}) } }//toolBar .accentColor(.red) } } } struct MockData { static var items = [ Item3(name: "Medical" ,color: .red, image:"heart"), Item3(name: "Illness" ,color: .blue, image:"pill"), // Illness Item3( name:"Vaccune" ,color: .orange, image: "syringe"), Item3(name: "Dewor" ,color: .green, image: "microbe"), Item3(name: "Allergie" ,color:.purple, image: "allergens")] } struct MedicalView: View { var body: some View { Text("Medical") } } struct IllnessView: View { var body: some View { Text("Illness") } } struct VaccuneView: View { var body: some View { Text("Vaccune") } } struct DeworView: View { var body: some View { Text("Dewor") } } struct AllergieView: View { var body: some View { Text("Allergie") } }
Topic: App & System Services SubTopic: Core OS Tags:
Replies
Boosts
Views
Activity
Apr ’24
Reply to Arranging Controller, Scenes & Views Structure
At first I thought, it would be a good idea to arrange multiple scenes, all controlled by the GameViewController, due to not having to duplicate recurring methods or so. But as I thought of, I saw a GameViewController file bigger and bigger and I had the fear to more and more loosing the focus the more scenes I added. It depends how similar are the functions in your scenes. But you can declare the function at the top level (outside any class) and call them from Views. Doing so, GameController will just segue to the appropriate View. Or you could put the fund in a 'Utils' class (maybe as static function) and call Utils.theFunc (if static) or create a Utils instance (utils: Utils) in each Scene (which will allow to adapt some properties) and call utils.myFunc. Hard to say more without knowing more on your code.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Apr ’24