Post

Replies

Boosts

Views

Activity

Reply to Binding<String>, set, get?
The following would work except that I'm required to use Binding. struct ContentView: View { @State var username = "" @State var password = "" @State var tenantID = "" var body: some View { VStack { makeForm(label: "Username: ", placeHolder: "123456", text: $username) makeForm(label: "Password: ", placeHolder: "abcdefg", text: $password) //makeForm(label: "Shop ID: ", placeHolder: "Amazon River Branch", text: $tenantID) HStack { Text("Shop ID: ") TextField("Amazon River Branch", text: $tenantID) .onChange(of: tenantID) { newValue in tenantID = textFilter(value: newValue) } } }.padding(.horizontal, 40.0) } @ViewBuilder private func makeForm(label: String, placeHolder: String, text: Binding<String>) -> some View { HStack { let newText = Binding<String>( get: { text.wrappedValue }, set: { text.wrappedValue = textFilter(value: $0) } ) Text(label) TextField(placeHolder, text: newText) } } func textFilter(value: String) -> String { let pattern = "[^A-Za-z0-9]+" return value.replacingOccurrences(of: pattern, with: "", options: [.regularExpression]) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’22
Reply to AVCaptureSession startRunning crash
I've ended up with the following lines of code. func prepareCamera(cameraCase: CameraCase) { /* removing existing layers */ if let sublayers = self.view.layer.sublayers { for sublayer in sublayers { if sublayer.isKind(of: AVCaptureVideoPreviewLayer.self) { sublayer.removeFromSuperlayer() } } } /* creating a capture session */ captureSession = AVCaptureSession() guard let device = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: cameraCase == .front ? .front : .back).devices.first else { return } let videoInput = try? AVCaptureDeviceInput(device: device) if captureSession.canAddInput(videoInput!) { captureSession.addInput(videoInput!) imagePhotoOutput = AVCapturePhotoOutput() // setting output destination captureSession.addOutput(imagePhotoOutput) // adding photo output to session } /* creating a capture layer */ let previewLayer: AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer.init(session: captureSession) previewLayer.videoGravity = AVLayerVideoGravity.resizeAspect /* adding video capture layer to the view layer */ self.view.layer.addSublayer(previewLayer) /* starting capture session */ DispatchQueue.global(qos: .background).async { self.captureSession.startRunning() } }
Topic: UI Frameworks SubTopic: UIKit Tags:
Dec ’22
Reply to List Single Selection
Thanks. I'm not sure how that helps, though. In the meantime, Paul from Hacking with Swift shows how the List guy works. I've tested the exact lines of code he has, and the selection symbol doesn't appear, either. That's odd. The article is not even one year old. By the way, I'm using Xcode 14.3. https://www.hackingwithswift.com/quick-start/swiftui/how-to-allow-row-selection-in-a-list
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jul ’23
Reply to Sorting CoreData Records by Creation Date
Thank you for your reply. Yes and no. I've figured out that I can do it by having an additional Date attribute. So I've started with a new Xcode project by having a new Date attribute named createdAt. And code goes as follows. import SwiftUI struct ContentView: View { @Environment(\.managedObjectContext) var managedObject @FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \Student.createdAt, ascending: true)]) var students: FetchedResults<Student> var body: some View { VStack { List(students) { student in Text(student.name ?? "") } Button { let firstNames = ... let lastNames = ... if let selectedFirstName = firstNames.randomElement(), let selectedLastName = lastNames.randomElement() { let newStudent = Student(context: managedObject) newStudent.id = UUID() newStudent.name = "\(selectedFirstName) \(selectedLastName)" newStudent.createdAt = Date() // <<<<<<<<<<<<<<<<<<<<<< try? managedObject.save() } } label: { Text("Add") } } } } , which takes care of the sort issue. As for the older project shown above, I wonder if Core Data has a simple mechanism in reordering the records when the entity does not include an additional Date attribute? That was my initial question.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jul ’23