great, it works. Thanks a lot. What a strange default behavior though.
Post
Replies
Boosts
Views
Activity
you could use something like this:
import SwiftUI
struct TestView: View {
@State var timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
@State var timeNow = ""
let dateFormatter = DateFormatter()
var body: some View {
Text("Currently: " + timeNow)
.onReceive(timer) { _ in
self.timeNow = dateFormatter.string(from: Date())
}
.onAppear(perform: {dateFormatter.dateFormat = "LLLL dd, hh:mm:ss a"})
}
}
This will display the time updated every second
don't understand your request, "...ensure it loads?"
I didn't know it sometimes does not work.
I'm using xcode 12.0-beta-3, target ios 13.5 and 14, tested on a few simulators and real devices, ipad(ios 14) and iphone(ios 13.5).
I cannot see any problems with the following (updated) test:
import SwiftUI
struct ContentView: View {
		var body: some View {
				TestView().padding()
		}
}
struct TestView: View {
		@State var timeNow = ""
		let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
		var dateFormatter: DateFormatter {
				let fmtr = DateFormatter()
				fmtr.dateFormat = "LLLL dd, hh:mm:ss a"
				return fmtr
		}
		
		var body: some View {
				Text("Currently: " + timeNow)
						.onReceive(timer) { _ in
								self.timeNow = dateFormatter.string(from: Date())
						}
		}
}
What setup does it fail on?
@OOper, good point. Do not use this code for precise time keeping or clocks synchronization.
Somewhere amongst the xcode 12 release notes, there is:
Known Issues
A newly-created iOS project using the Swift language may no longer build after enabling Mac Catalyst. (67885114)
Workaround: Replace the @main annotation on the App Delegate with @UIApplicationMain.
same issue for me on iPhone ios 14.2, but not on iPad 14.2, using xcode 12 and 12.2 beta.
you could try something like this:
						@State var theText = ""
						@State var isInSecureField = false
						SecureField("enter your password", text: Binding<String>(
								get: { self.theText },
								set: { self.theText = $0
										print("editing a SecureField")
self.isInSecureField = true		
									 }
								 ))
same issue here as well, 12.2
I've copied your code as is, into xcode 12.2 beta2 project, running on macos 11 beta, and all works well.
try turning off "Debug executable" in the active scheme,
Product->Scheme->Edit scheme->Run->Info
What errors do you get when you create a new project?
What system are you using?
You can do the following to setup a button to send a request.
Note: you need to use "https" otherwise you will have to fiddle with the security entitlements. import SwiftUI
	 struct ContentView: View {
		var body: some View {
		 Button(action: {sendHttpPost()}) {
				Text("Press here to send a post")
.padding(15)
.foregroundColor(.blue)
.border(Color.blue, width: 2)
}.buttonStyle(PlainButtonStyle())
}
func sendHttpPost() {
let session = URLSession(configuration: .default)
let url = URL(string: "http:...//ServerIP:8000/xmlcommand")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("text/plain", forHTTPHeaderField: "Content-Type") // I guess this can be "text/xml"
// Working line
request.httpBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Commands><command type=\"open\"><name>environments/Animals.hwe</name><id>a1</id></command></Commands>".data(using: .utf8)
let task = session.dataTask(with: request) { data, response, error in
// print(data as Any)
// print(response as Any)
// print(error as Any)
// do something with the result
print(data as Any? as Any)
if let data = data {
print(String(data: data, encoding: .utf8)!)
} else {
print("no data")
}
}
task.resume() // <- otherwise your network request won't be started
}
}
Michael,
the code is not for storyboard, it is for SwiftUI interface. You will need to keep reading on Swift projects and the differences. All the best with your initial steps.
it is common practice to use the string interpolation \(...) , such as this:
let servIp = "192.168.0.196" // String
let port = 8000 // Int
let cmd = "xmlcommand" // String
if let url = URL(string: "http://\(servIp):\(String(port))/\(cmd)") {
print("----> url: \(url)")
}
Maybe I'm missing something or not understanding the question, but I cannot see where in your code you change the opacity of the Button.