Post

Replies

Boosts

Views

Activity

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 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 Showing Multiple Instances of View and Showing Results from Them
I've figured it out accidentally. import SwiftUI struct ContentView: View { @ObservedObject var monster: Monster var body: some View { VStack { Button { print(monster.items) } label: { Text("Show me your current text strings") }.padding(.vertical, 10.0) ForEach($monster.items) { item in MyTextView(id: item.id, text: item.text).environmentObject(monster) }.padding(.horizontal, 200.0) Button { monster.items.append(Keyword()) } label: { Text("Add me!") }.padding(.vertical, 10.0) } } } class Monster: ObservableObject { @Published var items = [Keyword]() } struct Keyword: Identifiable { var id = UUID() var text = String() } struct MyTextView: View { @Binding var id: UUID @Binding var text: String @EnvironmentObject var monster: Monster var body: some View { VStack { HStack { TextField("Enter text", text: $text) Button { monster.items.removeAll(where: { $0.id == id} ) } label: { Text("Delete") } } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’22
Reply to Showing Multiple Instances of View and Showing Results from Them
I guess it's something close to the following. struct ContentView: View { @State var textMonsters = [MyTextView]() var body: some View { VStack { Button { print("Monsters \(textMonsters)") } label: { Text("Show me your current text strings") }.padding(.vertical, 10.0) ForEach(textMonsters, id: \.self) { textMonster in textMonster }.padding(.horizontal, 100.0) Button { textMonsters.append(MyTextView(id: UUID(), text: "ABC")) } label: { Text("Add me!") }.padding(.vertical, 10.0) } } } struct MyTextView: View, Identifiable, Hashable { var id = UUID() var text: String var body: some View { ZStack { TextField("Enter some text", text: Binding(get: { text }, set: { _ in })) } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’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:
Replies
Boosts
Views
Activity
Dec ’22
Reply to AVCaptureSession startRunning crash
Oops... The position argument is the other way around.
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Dec ’22
Reply to Installing macOS 13 RC
It looks like it cannot be used on a non-Apple silicon Mac.
Replies
Boosts
Views
Activity
Oct ’22
Reply to Sorting Array of Dictionaries with Exceptions?
I've used a custom sorting method with enum in reference to a topic at stack overflow.. (the one written by Martin R...)
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Sep ’22
Reply to Deleting a Row in the List with Data from Realm
when we use access a dataset in Realm -> when we access a dataset in Realm
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Sep ’22
Reply to Files Selected with UIDocumentPickerViewController Ending Up in File Provider Storage [SwiftUI]
It sounds like I've screwed up a big time according to RayWenderlich's article. https://www.raywenderlich.com/697468-ios-file-provider-extension-tutorial
Topic: App & System Services SubTopic: Core OS Tags:
Replies
Boosts
Views
Activity
Jul ’22
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:
Replies
Boosts
Views
Activity
May ’22
Reply to Binding<String>, set, get?
I think I cannot use Binding. Instead, I think I need to use TextField with .onChange.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’22
Reply to Binding<String>, set, get?
The warning will disappear with the following, but the text entry won't be filtered. set: { text.wrappedValue = filter(value: $0) }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’22
Reply to Binding<String>, set, get?
Currently, the warning says "Expression of type 'String' is unused."
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’22
Reply to Binding<String>, set, get?
Currently, what I type is not filtered.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’22
Reply to Showing Multiple Instances of View and Showing Results from Them
I've figured it out accidentally. import SwiftUI struct ContentView: View { @ObservedObject var monster: Monster var body: some View { VStack { Button { print(monster.items) } label: { Text("Show me your current text strings") }.padding(.vertical, 10.0) ForEach($monster.items) { item in MyTextView(id: item.id, text: item.text).environmentObject(monster) }.padding(.horizontal, 200.0) Button { monster.items.append(Keyword()) } label: { Text("Add me!") }.padding(.vertical, 10.0) } } } class Monster: ObservableObject { @Published var items = [Keyword]() } struct Keyword: Identifiable { var id = UUID() var text = String() } struct MyTextView: View { @Binding var id: UUID @Binding var text: String @EnvironmentObject var monster: Monster var body: some View { VStack { HStack { TextField("Enter text", text: $text) Button { monster.items.removeAll(where: { $0.id == id} ) } label: { Text("Delete") } } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’22
Reply to Showing Multiple Instances of View and Showing Results from Them
@Claude31 No, sir.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’22
Reply to Showing Multiple Instances of View and Showing Results from Them
I guess it's something close to the following. struct ContentView: View { @State var textMonsters = [MyTextView]() var body: some View { VStack { Button { print("Monsters \(textMonsters)") } label: { Text("Show me your current text strings") }.padding(.vertical, 10.0) ForEach(textMonsters, id: \.self) { textMonster in textMonster }.padding(.horizontal, 100.0) Button { textMonsters.append(MyTextView(id: UUID(), text: "ABC")) } label: { Text("Add me!") }.padding(.vertical, 10.0) } } } struct MyTextView: View, Identifiable, Hashable { var id = UUID() var text: String var body: some View { ZStack { TextField("Enter some text", text: Binding(get: { text }, set: { _ in })) } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’22
Reply to Showing Multiple Instances of View and Showing Results from Them
I need to change MyTextView() to MyTextView(text: "Hello")
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’22