Post

Replies

Boosts

Views

Activity

Reply to Switch Statements in SwiftUI
You can use comma, to use multiple patterns in a case: &#9;&#9;&#9;&#9;switch score { &#9;&#9;&#9;&#9;case 0, 25, 49: &#9;&#9;&#9;&#9;&#9;&#9;Image("gameboard0") &#9;&#9;&#9;&#9;case 1: &#9;&#9;&#9;&#9;&#9;&#9;Image("gameboard1") &#9;&#9;&#9;&#9;case 26..<30, 35...40, 50...: &#9;&#9;&#9;&#9;&#9;&#9;Image("gameboardX") &#9;&#9;&#9;&#9;default: &#9;&#9;&#9;&#9;&#9;&#9;Image("default") &#9;&#9;&#9;&#9;} Also, you can use range of Int when you need it.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’21
Reply to delegate / protocol not receiving messages, help!
The only thing I can think of is that I have two classes in the same file. My ViewController swift file has a View Controller class, and I'm also subclassing AVPlayerView in the same file, as my code shows. I don't know if that's the right or wrong way to do it. I'm not using any old tutorials.  That may not prevent ViewController to work, when other settings are normal. that viewDidLoad() is not being called. How have you confirmed that?
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’21
Reply to "Failed to produce a diagnostic for expression; please file a bug report" error.
What do you want to discuss or ask? If you want to write a bug report, this is not the place. Visit Feedback Assistant - https://developer.apple.com/bug-reporting/. If you want to ask how to fix, the code shown is not enough. Please show definitions of totalWater, SmallDailyWater, QueryWidget, DailyWaterMO and all other relevant types and properties. By the way, you have no need to use NSLocalizedString with Text. You can simply write: &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text("DailyWater", comment: "DailyWater") instead of: &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text(NSLocalizedString("DailyWater", comment: "DailyWater"))
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’21
Reply to delegate / protocol not receiving messages, help!
This code: > var customPlayerView = CustomAVPlayerView() and this description:  I did change the identity / class of my playerView to my customAVPlayerView class are inconsistent. When you set Custom Class on the storyboard, it is NOT you who instantiate the view controller or the view. iOS instantiates it. Do you remember you wrote this? I'm instantiating ANOTHER PlayerView, not the one I already have. With writing CustomAVPlayerView(), you ARE instantiating ANOTHER CustomAVPlayerView. You need to declare an @IBOutlet to receive the right instance which iOS created for you. &#9;&#9;@IBOutlet weak var customPlayerView: CustomAVPlayerView! If you have set up your storyboard correctly, you can connect your CustomAVPlayerView on the storyboard to the outlet declared above.
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’21
Reply to Compatible teaching book with Xcode
On page 61 there are three thumbnails (?) at the bottom of the page. If you click on the rightmost thumbnail, the field labeled User Interface contains Storyboard.  Thanks for telling me that. In fact, I hadn't noticed it. How clever iBook is! I am. now using the latest version of Xcode, 12.3. I tried using 11.7 again because I thought I had noticed some inconsistencies, but I could not run my app, only build it. There were no other targets devices available. There are some changes between 12.3 and 11.7, but not many. You know you can ask another question (or many other) if you cannot resolve by yourself.
Jan ’21
Reply to Instance method 'applicationDidFinishLaunching' nearly matches optional
Here is the code copy and pasted as is: p.s. it is linked with Boost lib (C++) Thanks for showing your code. Your method header is seemingly right. Can you try changing the method header as follows and tell us what happens? &#9;&#9;func applicationDidFinishLaunching(_ aNotification: AppKit.Notification) { //<- Use `AppKit.Notification` instead of `Notification`
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’21
Reply to Cannot convert value of type 'String' to type 'NSNumber' in coercion
In your code, after is a String already formatted. And outputFormatter is a MeasurementFormatter for Measurement, not for NSNumber. So, you have several options: Show after directly in line 80 -- Text("\(after)") (Or simply Text(after)) Change the definition of after to return Measurement<UnitTemperature>, and use the outputFormatter as is Text("\(after, formatter: outputFormatter)") (No as NSNumber) Change the definition of after to return some numeric type (such as Double), and change the outputFormatter to return NumberFormatter and use line 80 as is (With this option, you will loose unit from shown text.) ... Anyway, you need to make 3 things consistent: after outputFormatter -- this needs to be able to format the type of after line 80 -- if after is String, do not use outputFormatter. Else, you need to use the right as casting and the right formatter
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’21
Reply to Variable overriding itself when adding to a list
The word override can be interpreted in several ways, so I may be mistaking something, but your problem seems to be caused by your List in CardsView. &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;NavigationLink(destination: CardFullView(cname: cardsInfo.newCard.cname, name: cardsInfo.newCard.name, id: cardsInfo.newCard.id)) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;CardRow(cname: cardsInfo.newCard.cname, name: cardsInfo.newCard.name, id: cardsInfo.newCard.id) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} You use ForEach for cardsInfo.cards, receiving each card in card. But your CardRow always shows cardsInfo.newCard ignoring each card. So, every row in your List shows always the only CardInfo kept in cardsInfo.newCard. You may need to have an Array of CardInfo in your CardsInfo, and show it in your CardsView: CardsInfo struct CardInfo: Identifiable { //<- Needs `Identifiable` to use with `ForEach` &#9;&#9;var name: String = "" &#9;&#9;var id: String = "" &#9;&#9;var cname: String = "" } class CardsInfo: ObservableObject { &#9;&#9;@Published var newCard: CardInfo = CardInfo() &#9;&#9;@Published var cards: [CardInfo] = [] //<- Make this an Array of `CardInfo`, not of `Card` &#9;&#9; &#9;&#9;func add() { &#9;&#9;&#9;&#9;cards.append(newCard) //<- Add `newCard` to `cards` &#9;&#9;} } CardsView: &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;NavigationView { &#9;&#9;&#9;&#9;&#9;&#9;List { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text("\(sheetInfo.showSheetView.description)") &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;ForEach(cardsInfo.cards) { card in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;NavigationLink(destination: CardFullView(cname: card.cname, name: card.name, id: card.id)) { //<- &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;CardRow(cname: card.cname, name: card.name, id: card.id) //<- &#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;.onDelete(perform: onDelete) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.onMove(perform: onMove) &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;//... &#9;&#9;&#9;&#9;} &#9;&#9;} If this change causes something wrong, please try to explain that showing -- Steps to reproduce What you expect What you actually get
Topic: App & System Services SubTopic: General Tags:
Jan ’21
Reply to iOS Build fails when using vImage_CGImageFormat(cgImage: CGImage), even with target=13.0
Sorry, but I cannot reproduce the same issue in any build settings with Xcode 12.3. (I know you say "If I create a new project with these same settings the build works fine".) First of all, your demo code causes another error on line 7: Cannot find type 'UIImage' in scope I doubt if it is the actual code which causes the error: Undefined symbol. When shown code too simplified, it does not help solving your issue. And the latest version of Xcode currently is 12.3. 12.5 is not yet even in beta. Could it be related to other pods I have in the project? Very likely, but not sure. Starting from your the build works fine project, add pods (or other things in your causing the build error project) one by one and see what happens.
Jan ’21
Reply to Force color scheme at the press of a button
You can override system color scheme using the view modifier colorScheme: import SwiftUI struct ContentView: View { &#9;&#9;@Environment(\.colorScheme) var systemColorScheme &#9;&#9;@State var myColorScheme: ColorScheme? &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;NavigationView { &#9;&#9;&#9;&#9;&#9;&#9;VStack { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text("Hello, world!") &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;//... &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;NavigationLink("Settings", destination: SettingsView(colorScheme: $myColorScheme)) &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;//Override `colorScheme` &#9;&#9;&#9;&#9;.colorScheme(myColorScheme ?? systemColorScheme) &#9;&#9;} } struct SettingsView: View { &#9;&#9;@State private var selectedAppearance = 1 &#9;&#9;@Binding var colorScheme: ColorScheme? &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;// ... &#9;&#9;&#9;&#9;Picker(selection: $selectedAppearance, label: Text("Appearance")) { &#9;&#9;&#9;&#9;&#9;&#9;Text("System Default").tag(1) &#9;&#9;&#9;&#9;&#9;&#9;Text("Light").tag(2) &#9;&#9;&#9;&#9;&#9;&#9;Text("Dark").tag(3) &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;.onAppear { &#9;&#9;&#9;&#9;&#9;&#9;switch colorScheme { &#9;&#9;&#9;&#9;&#9;&#9;case .none: &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;selectedAppearance = 1 &#9;&#9;&#9;&#9;&#9;&#9;case .light: &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;selectedAppearance = 2 &#9;&#9;&#9;&#9;&#9;&#9;case .dark: &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;selectedAppearance = 3 &#9;&#9;&#9;&#9;&#9;&#9;default: &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;break &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;.onChange(of: selectedAppearance) { value in &#9;&#9;&#9;&#9;&#9;&#9;//print(selectedAppearance) &#9;&#9;&#9;&#9;&#9;&#9;switch selectedAppearance { &#9;&#9;&#9;&#9;&#9;&#9;case 1: &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;//print("System Default") &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;colorScheme = nil &#9;&#9;&#9;&#9;&#9;&#9;case 2: &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;//print("Light") &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;colorScheme = .light &#9;&#9;&#9;&#9;&#9;&#9;case 3: &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;//print("Dark") &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;colorScheme = .dark &#9;&#9;&#9;&#9;&#9;&#9;default: &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;break &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;// ... &#9;&#9;} }
Topic: App & System Services SubTopic: General Tags:
Jan ’21
Reply to Calling a property of a struct with a placeholder?
The word placeholder does not seem to be a good one to represent your issue. Many languages have some feature to access properties indirectly, in Swift, it is called as keyPath. But Swift is statically typed language and its usage is a little bit difficult: import Foundation struct Horse { &#9;&#9;var name: String &#9;&#9;var basicTraining : Float &#9;&#9;var rhythm : Float &#9;&#9;var suppleness : Float } var myHorses = [ &#9;&#9;Horse(name: "Donnerhall", basicTraining : 0.5, rhythm : 0.2, suppleness : 0.1), &#9;&#9;Horse(name: "Bjork", basicTraining : 0.4, rhythm : 0.3, suppleness : 0.1) ] var horseIndex = 0 var currentTraining: WritableKeyPath<Horse, Float> = \.basicTraining //or \.rhythm or \.suppleness func trainHorse() { &#9;&#9;if myHorses[horseIndex][keyPath: currentTraining] > 100 { &#9;&#9;&#9;&#9;myHorses[horseIndex][keyPath: currentTraining] = 100 &#9;&#9;} &#9;&#9;if myHorses[horseIndex][keyPath: currentTraining] < 100 { &#9;&#9;&#9;&#9;myHorses[horseIndex][keyPath: currentTraining] += currentScaleCode[skillIndex].basicBoost &#9;&#9;} &#9;&#9;//... } You are not showing relevant definitions in The struct, arrays and variables. So, I do not see how to write currentBoost using keyPath.
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’21
Reply to SwiftUi strange behaviour (bugs?)
If I put more than ten subviews into VStack/HStack/ScrollView/List, I got error "Extra argument in call", but there is no extra argument (actually, I get this error if I just add 11 Text("Hello, world!")) It is a know limitation of the current implementation of SwiftUI. The Swift language currently does not support variadic generic parameters. So, in the implementation @ViewBuilder, can only get 10 arguments. I get this error if I just add 11 Text("Hello, world!") That Text("Hello, world!") definitely is the extra argument. All the views declared in VStack/HStack/ScrollView/List... are passed to a hidden method, each view as an argument. If I put List into ScrollView, this list is not rendered (just empty space instead it), but there is no any error/warning/log messages (like for wrong constraints) This is another known issue. You can find some articles searching with "swiftui list in scrollview". List hides when adding in ScrollView SwiftUI - https://stackoverflow.com/a/58969900/6541007 (Seems this is not as famous as the 10-view-limitation.) You can send some feedback about these issues, whether they are known or not.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’21