Post

Replies

Boosts

Views

Activity

Reply to Any examples of IKSaveOptions() / NSSavePanel() filetype selectors out there?
let me know if something rings a bell? You are not using the documented initializer of IKSaveOptions. init(imageProperties:imageUTType:) - https://developer.apple.com/documentation/quartz/iksaveoptions/1503412-init let panel = NSSavePanel() let options = IKSaveOptions(imageProperties: [:], imageUTType: kUTTypeImage as String)! options.addAccessoryView(to: panel) panel.begin { response in if response == .OK, let savePath = panel.url { //... } }
Topic: UI Frameworks SubTopic: AppKit Tags:
Feb ’21
Reply to Change VStack color with a button inside an HStack
Why don't you set the background color in the action of the button? struct ContentView: View { 		 		@State var index = 0 		@State var vstackColor: Color? = .black 		 		var body: some View { 				VStack{ 						Spacer() 						CustomTabs(index: $index, 											 color: $vstackColor) 				}.background(vstackColor ?? Color.black) 		} } struct CustomTabs : View { 		 		@Binding var index : Int 		@Binding var color: Color? 		 		var body: some View { 				HStack{ 						Button(action: { 								self.index = 0 								self.color = .red //Other color 						}) { 								Spacer() 								Image("fdt") 								Spacer() 						} 				} 		} }
Feb ’21
Reply to Data.withUnsafeMutableBytes memory lifetime
Is it safe to assume that as long as my Swift object has a reference to the Data, the pointer will remain valid? Or, might Swift optimize it away? It is clearly documented such usage is unsafe: withUnsafeMutableBytes(_:) - https://developer.apple.com/documentation/foundation/data/1779823-withunsafemutablebytes Warning The byte pointer argument should not be stored and used outside of the lifetime of the call to the closure. f so, is there any way to insure the data remains available, other than making a copy within the C library?  You can make a copy within Swift: class EMsg { &#9;&#9;var em: UnsafeMutablePointer<T_EMsg>? &#9;&#9;var byteBuf: UnsafeMutableBufferPointer<UInt8> &#9;&#9; &#9;&#9;init?(_ data:Data) { &#9;&#9;&#9;&#9;self.byteBuf = .allocate(capacity: data.count) &#9;&#9;&#9;&#9;_ = self.byteBuf.initialize(from: data) &#9;&#9;&#9;&#9;self.em = emsg_read(byteBuf.baseAddress, byteBuf.count); &#9;&#9;} &#9;&#9; &#9;&#9;deinit { &#9;&#9;&#9;&#9;byteBuf.deallocate() &#9;&#9;&#9;&#9;//Some code to free em? &#9;&#9;&#9;&#9;//... &#9;&#9;} &#9;&#9; &#9;&#9;func process()&#9;{ &#9;&#9;&#9;&#9;guard let em = em else { return } &#9;&#9;&#9;&#9;emsg_process(em) &#9;&#9;} }
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21
Reply to Setting UILabels to Firestore data
”!=“ is not, right? Thats what I have learned.  Didn't you read my replies? Your code (updated one): &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;if snapshot != nil { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;//Doing nothing when `snapshot` is *not nil* &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;}else { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;//This code block is executed when `snapshot` *is nil* &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;//... &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} is equivalent to: &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;if snapshot == nil { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;//This code block is executed when `snapshot` *is nil* &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;//... &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} You may need to learn how if-else works,
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21
Reply to Find which button is pressed in a Grid in SwiftUI
Is there a way to actually get the type of the enum here? So I can do something like this. Unfortunately, with declaring var onButtonTap: ((MenuItem) - Void)!, you are disposing the type info to tell to Swift compiler. (By the way, you should not use implicitly unwrapped Optional here. Better use explicit Optional or non-Optional.) You can make MenuViewObservable generic: class MenuViewObservableItemType: MenuItem: ObservableObject { var onButtonTap: ((ItemType) - Void) = {_ in} } But this may or may not work depending on the actual usage of observable.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’21
Reply to Find which button is pressed in a Grid in SwiftUI
btw I changed the ObservableObjects to use generics. It doesn't seem to have an effect on functionality-wise.  When you make MenuViewObservable generic, better use the generic type as the argument type of the closure: class MenuViewObservableT: MenuItem: ObservableObject { var onButtonTap: ((T) - Void) = { _ in } //- Use `T`, not `MenuItem` } Shows the following error. I cannot reproduce the same error with your currently shown code. Something hidden in your project may be affecting. For example, don't you have another Menu type in you project, including imported modules?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’21
Reply to Adding stacks to a stack view programmatically
it will add a horizontal stack view containing a label and a stepper Why don't you do it as you describe? &#9;&#9;func addButtons() { &#9;&#9;&#9;&#9;for (key,value) in feedInventory where value > 0 { &#9;&#9;&#9;&#9;&#9;&#9;let filteredProducts = feedProducts.filter { $0.code.contains(key)} &#9;&#9;&#9;&#9;&#9;&#9;for feedProduct in filteredProducts { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;let horizontalStack = UIStackView() &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;horizontalStack.axis = .horizontal &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;horizontalStack.alignment = .firstBaseline &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;let label = UILabel(frame: .zero) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;label.text = " \(feedProduct.name) , amount: \(value)" &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;//... &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;label.widthAnchor.constraint(equalToConstant: 200).isActive = true &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;horizontalStack.addArrangedSubview(label) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;let stepper = UIStepper(frame: .zero) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;//... &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;stepper.accessibilityLabel = "\(feedProduct.code) up/down" &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;horizontalStack.addArrangedSubview(stepper) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;feedStack.addArrangedSubview(horizontalStack) &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;} &#9;&#9;}
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21
Reply to change binding of List with button press
I may be missing something, but why don't you simply add an @State variable to choose the lists? struct SplitView: View { &#9;&#9;@EnvironmentObject var viewRouter: ViewRouter &#9;&#9;@State var list1 = [MyModel]() &#9;&#9;@State var list2 = [MyModel]() &#9;&#9; &#9;&#9;@State var selected: Int = 1 &#9;&#9;private var viewingModels: [MyModel] { &#9;&#9;&#9;&#9;switch selected { &#9;&#9;&#9;&#9;case 1: &#9;&#9;&#9;&#9;&#9;&#9;return list1 &#9;&#9;&#9;&#9;case 2: &#9;&#9;&#9;&#9;&#9;&#9;return list2 &#9;&#9;&#9;&#9;default: &#9;&#9;&#9;&#9;&#9;&#9;return [] &#9;&#9;&#9;&#9;} &#9;&#9;} &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;HStack { &#9;&#9;&#9;&#9;&#9;&#9;Button(action: { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;selected = 1 &#9;&#9;&#9;&#9;&#9;&#9;}){ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text("List 1") &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;Button(action: { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;selected = 2 &#9;&#9;&#9;&#9;&#9;&#9;}){ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text("List 2") &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;List(viewingModels, id: \.ID) { myitem in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;HStack(alignment: .center) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;VStack(alignment: .leading) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text(myitem.Name) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.font(.subheadline) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.fontWeight(.bold) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Spacer() &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;.onAppear(perform: loadList) &#9;&#9;&#9;&#9;Spacer() &#9;&#9;} &#9;&#9; }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’21
Reply to Setting UILabels to Firestore data
This is the most up to date code Thanks for updating the code. I have no knowledge and no intention to go deep into Firestore, but the lines 56...74 of your code is equivalent to the following: &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;if snapshot == nil { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;if let currentRiddle = snapshot?["CurrentRiddle"] as? String { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.CurrentRiddle.text = "This Weeks Riddle: " + currentRiddle &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;print(currentRiddle) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;if let previousRiddle = snapshot?["CurrentRiddle"] as? String { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.PreviousRiddle.text = "Previous Riddle: " + previousRiddle &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} The two if-lets are executed only when snapshot is nil, so, it has no meaning. You might want to do something like this: &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;if let snapshot = snapshot { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;if let currentRiddle = snapshot["CurrentRiddle"] as? String { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.CurrentRiddle.text = "This Weeks Riddle: " + currentRiddle &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;print(currentRiddle) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;if let previousRiddle = snapshot["CurrentRiddle"] as? String { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.PreviousRiddle.text = "Previous Riddle: " + previousRiddle &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;}
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21
Reply to Beginner's question: Transferring MKPointAnnotation title to a new VC
Why don't you utilize the sender of the segue? func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { self.performSegue(withIdentifier: "toDetailsVC", sender: view.annotation) //- } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "toDetailsVC" { guard let destinationVC = segue.destination as? DetailsVC, let annotation = sender as? MKPointAnnotation else { print("Unexpected segue: \(segue), \(String(describing: sender))") return } destinationVC.detailsTitle = annotation.title } }
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21
Reply to Conditionally changing a value in an array
Even when you apply var to the control variable of for-in statement, the variable holds the copy of each element of the Array as its initial value. You cannot change the original Array when you just modify the control variable. You need to write index based access when you want to modify the Array: for index in myHorses[horseIndex].training.indices { if myHorses[horseIndex].training[index] 1.0 { print("value 1.0 found") myHorses[horseIndex].training[index] = 1.0 } }
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21
Reply to DES in Swift using CommonCrypto
Assuming you can supply all other parameters, you may need to write something like this: var result = Data(count: maximumSizeOfDecryptedData) var numBytesDecrypted: Int = 0 let err = result.withUnsafeMutableBytes {bufPtr in CCCrypt(op, alg, options, key, keyLength, iv, dataIn, dataInLength, bufPtr.baseAddress, bufPtr.count, &amp;numBytesDecrypted) } if err == kCCSuccess { result.count = numBytesDecrypted print(result) //- Use `result` as the decrypted Data } else { print("error: \(err)") }
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21
Reply to progress bar will not work
What do you mean by will not work? If your code causes build time errors, you should show all the error messages. If your code causes runtime errors, you should include all the error info as well as all the steps to reproduce the error. If your code runs without errors but generates unexpected results, you need to clarify what you expect, what you actually get and how you can reproduce the issue.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’21