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())
Topic: UI Frameworks SubTopic: SwiftUI Tags:
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() } }
Topic: App & System Services SubTopic: General Tags:
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
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’21
Reply to Can I use CloudKit with apps built with Swift Playgrounds 4 on an iPad?
Even when I open the App project of Swift Playground 4 with Xcode 13.2.1, I cannot find iCloud in the capabilities list shown clicking + Capability button. So, I guess it might be one of the not yet features of Swift Playgrounds 4. But not sure as many things of Swift Playgrounds 4 are not yet revealed. I do appreciate if Apple's engineer could clarify.
Topic: App & System Services SubTopic: Hardware Tags:
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) } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’21
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())
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
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() } }
Topic: App & System Services SubTopic: General Tags:
Replies
Boosts
Views
Activity
Dec ’21
Reply to Print something on the screen
You need to define an area to output something, maybe a Text somewhere in the View.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Dec ’21
Reply to Images don't display when orientation is changed
Can you show us a complete code to reproduce the issue?
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Dec ’21
Reply to Text background color
.background(Color) changes all the background's color Can you clarify what is all the background's color and what is the text background color?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Dec ’21
Reply to Swift Playgrounds app
Every distribution method which Apple does not guarantee would cause any sort of troubles and you should not rely on them. If you are a member of paid Developer Program, you can add your friends as external testers on TestFlight.
Replies
Boosts
Views
Activity
Dec ’21
Reply to Why can’t I open the app created in swift playground 4 on the iPad in swift playground 4 on the mac?
Are you trying to open an App project of Swift Playgrounds 4 on iPad? Then you need to use Xcode (13.2 or later), not Swift Playgrounds 4 on Mac as for now.
Replies
Boosts
Views
Activity
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.
Replies
Boosts
Views
Activity
Dec ’21
Reply to Xcode 13.2.1
Do you have anything to ask?
Replies
Boosts
Views
Activity
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
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Dec ’21
Reply to function "picker " can't be found and I don't understand why
In your code, picker is a local variable inside viewDidLoad(). So, the scope of your picker is only inside viewDidLoad(). You may need to declare an instance variable picker, and assign your picker to it in viewDidLoad(). self.picker = picker
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Dec ’21
Reply to Set two var‘s after each other
Sorry, I have been missing the part where you use popover using selectedPlaceName.
Replies
Boosts
Views
Activity
Dec ’21
Reply to Can I use CloudKit with apps built with Swift Playgrounds 4 on an iPad?
Even when I open the App project of Swift Playground 4 with Xcode 13.2.1, I cannot find iCloud in the capabilities list shown clicking + Capability button. So, I guess it might be one of the not yet features of Swift Playgrounds 4. But not sure as many things of Swift Playgrounds 4 are not yet revealed. I do appreciate if Apple's engineer could clarify.
Topic: App & System Services SubTopic: Hardware Tags:
Replies
Boosts
Views
Activity
Dec ’21
Reply to Set two var‘s after each other
Seems you need to show more code. What am I doing wrong? One thing clearly wrong is that you are not showing enough code.
Replies
Boosts
Views
Activity
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) } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Dec ’21