Post

Replies

Boosts

Views

Activity

Reply to Print something on the screen
I've created a text and I would like that the text will change on the press of the button. For example: there's a Text() witch now has written on it "text" and when I press the blue button (you can see it in my code), I want "text" to change to number "5" Thanks for showing your code. You just need to declare an @State variable to hold the content of the Text. (Defining a struct NumberView makes your code too complex, better remove it.) import SwiftUI //<- import PlaygroundSupport let columns: [GridItem] = [GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible())] struct ContentView: View { @State var outputText: String = "text" //<- var body: some View { GeometryReader { geometry in VStack { Spacer(minLength: 290) Text(outputText) //<- .font(.largeTitle) ZStack { LazyVGrid(columns: columns) { Group { Button { outputText = "\(5)" //<- } label: { ZStack { Circle() .frame(width: 90, height: 90) .foregroundColor(Color(.systemBlue)) .opacity(0.5) //.position(x: geometry.size.width/2, y: -100) Text("5") .font(.largeTitle) .bold() .foregroundColor(.black) } .frame(width: 90, height: 90) } } } } } } } } PlaygroundPage.current.setLiveView(ContentView())
Jan ’22
Reply to Decoding JSON coming from MySQL via PHP to a complex Struct in SwiftUI
The most important part of the error message is this: CodingKeys(stringValue: "date", intValue: nil)], debugDescription: "Expected to decode Double but found a string/data instead." In your JSON text, the value for "date" is string, but in your struct, you are trying to pass the value to Date not String. What you may need is not a Numberformatter, but setting dateDecodingStrategy: final class ModelDataDepot: ObservableObject { @Published var depots = [Depot]() private let dateFormatter: DateFormatter = { let df = DateFormatter() df.dateFormat = "yyyy-MM-dd" df.locale = Locale(identifier: "en_US_POSIX") return df }() init() { let url = URL(string: "https://api.webcoders.ch/index.php")! URLSession.shared.dataTask(with: url) { (data, response, error) in if let error = error { print(error) return } guard let data = data else { print("No Data!") return } do { let decoder = JSONDecoder() decoder.dateDecodingStrategy = .formatted(self.dateFormatter) let decodedData = try decoder.decode([Depot].self, from: data) DispatchQueue.main.async { self.depots = decodedData } } catch { print("JSON-Error: \(error)") } }.resume() } }
Dec ’21
Reply to Xcode 13.2.1 update is stuck
Difficulties installing Xcode make us developer irritated, but one thing important is being patient and wait. I have experienced several times it took 3 hours to install recent Xcode and some report more than 6 hours. You may need to keep untouched for such a period. If you have ever tried waiting for more than 6 hours, you may try to install it by downloading .xip file. Visit the More Downloads page and download the latest Xcode.xip .This would also take as much time as App Store, but the installing process is a little more visible.
Dec ’21
Reply to Implement swift API for C++ multi-type structure
can I use the swift Class in objective-c in order to fill it up according to the CPP serverResponse ? You can make your class Objective-C compatible by adding @objc: @objc @objcMembers open class ServerResponseSwift : NSObject { open var error: ErrorMessageSwift open var strValue: String open var intVal: UInt16 //open var status: EnumValSwift init(error: ErrorMessageSwift, strValue: String, intVal: UInt16 ) { self.error = error self.strValue = strValue self.intVal = intVal //self.status = ... } } And you can use it in your Objective-C++ code : #import "SwiftAPICpp-Swift.h" @implementation Converter - (ServerResponseSwift *) getServerStatusSwift { serverResponse x = getServerResponse(); //... ServerResponseSwift *serverResponseSwift = [[ServerResponseSwift alloc] init...]; } @end
Dec ’21
Reply to Accessing variables in one class from other class
If you want to access some variables common from within the parent view and from within the child view, you may need to hold the variables in the parent view. As Swift View is a struct and modifying the variable inside its instance may cause many behaviors you do not expect. Child View import SwiftUI struct AddNewPresets: View { @Binding var username: String var emailFieldIsFocused: FocusState<Bool>.Binding var body: some View { TextField( "User name (email address)", text: $username ) .focused(emailFieldIsFocused) .onSubmit { //validate(name: username) } .textInputAutocapitalization(.never) .disableAutocorrection(true) .border(.secondary) } } Parent View import SwiftUI struct ContentView: View { @State var username: String = "" @FocusState var emailFieldIsFocused :Bool var body: some View { HStack { AddNewPresets(username: $username, emailFieldIsFocused: $emailFieldIsFocused) Text(username) .foregroundColor(emailFieldIsFocused ? .red : .blue) } } }
Dec ’21