Post

Replies

Boosts

Views

Activity

Reply to SwiftUI - What is Identifiable?
Does the compiler automatically generate a unique string like UUID for each element in the array or something? NO. In the code shown, it is the case you need to provide an id parameter to the ForEach initializer. Can I somehow print the raw value of each id? In your case, you specify \.self for id:, meaning -- The id of "Susan" is "Susan" itself. The id of "Kate" is "Kate" itself. The id of "Natalie" is "Natalie" itself. ... And so on. When you define an Identifiable struct explicitly, for example: struct User: Identifiable { var id: String {name} var name: String } Then you have no need to specify id: in ForEach: struct ContentView: View { var users: [User] = ["Susan", "Kate", "Natalie", "Kimberly", "Taylor", "Sarah", "Nancy", "Katherine", "Nicole", "Linda", "Jane", "Mary", "Olivia", "Barbara"] .map(User.init(name:)) var body: some View { List { ForEach(users) { user in Text(user.name) } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’21
Reply to MacOs - NSTableView, editing single cell
I'm using a cell based NSTableView. You should better read this old article: Programmatically Editing Data in an NSCell-Based Table Seems you need to implement tableView(_:setObjectValue:for:row:) in your NSTableViewDataSource: func tableView(_ tableView: NSTableView, setObjectValue object: Any?, for tableColumn: NSTableColumn?, row: Int) { guard let stringValue = object as? String else { return } //Check `tableColumn` in a real implementation... print("cell was edited") print(stringValue) infoForTableView[row] = stringValue } Generally, NSCell-based table view is sort of an older part of NSTableView, it is simpler in many simple cases, but less customizable.
Topic: UI Frameworks SubTopic: AppKit Tags:
Sep ’21
Reply to Rotate View around an other View SwiftUI
Please try this: struct MyGameView: View { @State private var degress: Double = 0 let timer = Timer.publish(every: 0.05, on: .main, in: .common).autoconnect() var body: some View { VStack{ ZStack{ Circle() .frame(width: 80) ZStack{ Circle() .stroke(lineWidth: 1) .frame(width: 300) BallonShape() .scaledToFit() .scaleEffect(0.2) .foregroundColor(.red) .offset(x: 0, y: -170) //Uncomment to clarify what is rotating around what //.background(Color(.displayP3, red: 0.7, green: 0, blue: 0.3, opacity: 0.5)) .rotationEffect(.degrees(degress), anchor: .center) //<- Move `rotationEffect` after `offset` and use `.center` } } } .onReceive(timer) { input in withAnimation(.easeIn(duration: 0.05).speed(10)){ degress += 1 } } } } You should better try with various sizes of devices or simulators to check if the code works as expected in all the sizes.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’21
Reply to Is it possible to replicate @State or @FetchRequest behaviour?
Or in a more concrete way: how can I send a state change from my own property wrapper so the view hierarchy is updated as response? Maybe you can achieve what you want using DynamicProperty. A simplified example: import SwiftUI struct ContentView: View { @Delay(seconds: 3) var value var body: some View { VStack { Text("\(value)") .padding() Button("add delayed") { value += 1 } } } } @propertyWrapper struct Delay: DynamicProperty { @State var value: Int = 0 let seconds: Int var wrappedValue: Int { get { value } nonmutating set { DispatchQueue.main.asyncAfter(deadline: .now() + DispatchTimeInterval.seconds(seconds)) { self.value = newValue } } } } Or this article might be a good example: https://www.hackingwithswift.com/plus/intermediate-swiftui/creating-a-custom-property-wrapper-using-dynamicproperty
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’21
Reply to Problems with NavigationView and NavigationLink
I cannot find how to submit a Xcode bug report. You use Apple's Feedback Assistant to report a bug. Please do not forget to include: Your environment -- not only Xcode version, your macOS version your Mac model, simulator/device OS version & model, etc... Issue-reproducible project Step-by-step instruction to reproduce the issue What you expect What actually happens Some Apple's engineers may want to know the bug report number and you can share it writing here.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’21
Reply to How to tap a row in a UIPickerView
How can I get the value of when the user taps the row? If you do not like adding a "Select" button, you can add a UITapGestureRecognizer to the UIPickerView. Something like this: import UIKit class ViewController: UIViewController { @IBOutlet weak var picker: UIPickerView! @IBOutlet weak var yourSquare: UILabel! var pickerData: [String] = [ "10", "20", "30" ] var squaresTaken: [Int] = [] override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. picker.delegate = self picker.dataSource = self let tap = UITapGestureRecognizer(target: self, action: #selector(pickerTapped)) tap.delegate = self picker.addGestureRecognizer(tap) } func intToString() { //??? } @objc func pickerTapped(_ gestureRecognizer: UITapGestureRecognizer) { guard let pickerView = gestureRecognizer.view as? UIPickerView else { return } let row = pickerView.selectedRow(inComponent: 0) squaresTaken.append(Int(pickerData[row])!) yourSquare.text = pickerData[row] } } extension ViewController: UIPickerViewDelegate, UIPickerViewDataSource { func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { pickerData.count } func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { intToString() return pickerData[row] } } extension ViewController: UIGestureRecognizerDelegate { func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { return true } }
Topic: Programming Languages SubTopic: Swift Tags:
Sep ’21
Reply to Can't Download Xcode 12.4
I tried and it stopped at 2.60GB with unknown error. Generally, just around iPhone event (and WWDC) is not a good time to download huge things. Apple's servers get super-unstable in such period. Wail about a week after new iOS is published and try again. Maybe Apple needs to hire better network engineers.
Sep ’21
Reply to How to add swift availability check to Environment values?
You may need to add @available on the struct using the new Environment value, and choose it using if #available. Something like this: struct MyView_watchOS7OrLower: View { var body: some View { Circle() .fill(Color.blue) } } @available(watchOSApplicationExtension 8.0, *) struct MyView_watchOS8OrHigher: View { @Environment(\.isLuminanceReduced) var isLuminanceReduced var body: some View { if isLuminanceReduced { Circle() .stroke(Color.gray, lineWidth: 10) } else { Circle() .fill(Color.white) } } } @ViewBuilder func MyView() -> some View { if #available(watchOS 8.0, *) { MyView_watchOS8OrHigher() } else { MyView_watchOS7OrLower() } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’21