Post

Replies

Boosts

Views

Activity

Reply to How can I display a random view?
It's because Swift doesn't know what type the value in screens is. If you used this: @State var screens: Array<FirstScreen> = [FirstScreen(), FirstScreen(), FirstScreen()] Swift would know that screens is an array of FirstScreen, and so it can supply that view to ContentView. However, that's clearly not very useful, so you need to wrap it in AnyView. Here you go: struct ContentView : View { @State var screens: [Any] = [FirstScreen.self, SecondScreen.self, ThirdScreen.self] var body: some View { self.buildView(types: self.screens, index: Int.random(in: 0..<screens.count)) } func buildView(types: [Any], index: Int) -> AnyView { switch types[index].self { case is FirstScreen.Type: return AnyView(FirstScreen()) case is SecondScreen.Type: return AnyView(SecondScreen()) case is ThirdScreen.Type: return AnyView(ThirdScreen()) default: return AnyView(FirstScreen()) } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’22
Reply to Logger on Xcode console
If you put the #if DEBUG ... #endif around the struct Logger { ... } you're telling the compiler to only include that code if the app is being built for DEBUG, not RELEASE, so it will fail if you try to build for RELEASE. Regardless of how the app is going to be built if you're calling a function that function has to exist. You simply need to move the #if DEBUG ... #endif to the bit that should only be printed when you're in DEBUG mode, i.e.: struct Logger { let subsystem: String let category: String func debug(_ message: String) { #if DEBUG print(message) #endif } } So, the code will compile and the message will only be printed if you're in DEBUG.
Nov ’22
Reply to Logger on Xcode console
Well of course it doesn't work. Even though you've got import os at the top of the file, by creating struct Logger further down you've ignored the os Logger entirely and used the one you've just created in your file, so it doesn't know how to interpolate \(itemName, privacy: .public). If you want to use the os Logger, you need to remove your struct Logger. I can't understand why you'd want to remove the timestamps, and if you do seriously need to do this, you'll have to use an extension and override the message, which might be difficult to do and gain you very little.
Nov ’22
Reply to Background image scales incorrectly when ignoring safe area
The order of effects is important. This works: struct ContentView: View { var body: some View { ZStack { Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.").padding() } .frame(maxHeight: .infinity) .background(Image("dots") .resizable() .scaledToFill() // - These two were swapped around .ignoresSafeArea() // - ) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’22
Reply to Operator function '<' requires that 'Date' conform to 'StringProtocol'
I think @Claude31's answer is helpful, but I personally would not do a String comparison for Dates. I think you should make sure listDate is a Date, not a String. Here's a chunk of code that shows how the comparisons with Strings can be wrong if the format isn't always right: let dateFormatter = DateFormatter() struct ContentView: View { var body: some View { VStack { // Trying with YY/MM/dd Strings. // This works because the String goes highest number to lowest number, i.e. years then months then days. if("27/12/2022" < "28/11/2022") { Text("'27/12/2022' < '28/11/2022'") } else { Text("'27/12/2022' >= '28/11/2022'") } // Now try with a different date format, i.e. "dd/MM/yyyy". // This won't work because the 1st Dec 2022 is lower than the 2nd January 2022 because it begins with a 1. if("01/12/2022" < "02/01/2022") { Text("'01/12/2022' < '02/01/2022' = incorrect") } else { Text("'01/12/2022' >= '02/01/2022' = correct") } // Now try the comparison with Dates taken from Strings let date1String: String = "01/12/2022T15:00:00+0000" let date2String: String = "02/01/2022T15:00:00+0000" let date1Date: Date = getDateFromString(date1String) let date2Date: Date = getDateFromString(date2String) if(date1Date < date2Date) { Text("'01/12/2022' < '02/01/2022' = incorrect") } else { Text("'01/12/2022' >= '02/01/2022' = correct") } } } func getDateFromString(_ dateString: String) -> Date { dateFormatter.dateFormat = "dd/MM/yyyy'T'HH:mm:ssZ" return dateFormatter.date(from: dateString)! } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’22
Reply to How do I dismiss the DatePicker popup in UI tests that are running on iOS 16 devices?
It's because the object you're trying to tap() isn't Hittable - you can check that by adding an assertTrue. Here's a way to do it using an extension: extension XCUIElement { func forceTap() { if(self.isHittable) { self.tap() } else { let coordinate: XCUICoordinate = self.coordinate(withNormalizedOffset: CGVector(dx:0.0, dy:0.0)) coordinate.tap() } } } And call it with application.datePickers["DatePicker"].forceTap().
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’22
Reply to Bug in sed
Oh, I get you now. Yes, but doesn't that suggest that the characters you're using aren't UTF-8? For example, this works: Command: echo ø | LANG=ISO-8859-1 sed -e 's/^/o/g' Output: oø
Topic: App & System Services SubTopic: Core OS Tags:
Nov ’22