Post

Replies

Boosts

Views

Activity

Reply to What is _VectorMath protocol? Conforming to VectorArithmetic and AdditiveArithmetic
What is _VectorMath protocol? Not documented publicly. Where is it defined? Somewhere in the SwiftUI framework, maybe. (Using _VectorMath causes error without import SwiftUI.) Is it ok to use it like this? NO. Considering the fact that it does not appear in the generated header of imported frameworks and that Xcode does not show any code completion of it, it is considered to be intentionally hidden. (For some reason, it cannot be private.) So, you should better take it as a private API that Apple inhibits us to use. Or should I use a different approach? I think you should. Just my curiosity, how are you trying to make Array conform to VectorArithmetic? For example, what is your definition of +? Two Arrays may have different sizes.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20
Reply to Beginners Question (Expressions are not allowed at the top level)
Executable statements, such as TextLabel1.setLabel(inputText: "Hello World") are not allowed to be placed on the top level, as the error message is saying. Such statements needs to be in a function or some closure. import Foundation let textLabel1 = ViewController() func mySetLabel() { textLabel1.setLabel(inputText: "Hello World") //<- This is not at the top level } This will compile, but may not run as you expect. how can I call this function? It depends on when you want to call the function.
Dec ’20
Reply to List and MysqlDB
The error is: " cannot use instance member "f" within property initializer " As the error message is stating, you cannot use f (@Binding var in BarberView) in the initial value of fetcher. How to fix depends on how you are using BarberView and how you want to pass the value of f. Can you explain these?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20
Reply to Convert with Measurement SwiftUI
(Is there a way to use NumberFormatter with the computed String) When you create a String for showing Measurement to users, you should better use MeasurementFormatter when converting to String. Something like this: struct PowerView: View { &#9;&#9;let outputFormatter: MeasurementFormatter = { &#9;&#9;&#9;&#9;let nf = NumberFormatter() &#9;&#9;&#9;&#9;//Set number style as you like... &#9;&#9;&#9;&#9;nf.numberStyle = .none &#9;&#9;&#9;&#9;nf.maximumSignificantDigits = 15 &#9;&#9;&#9;&#9;nf.usesSignificantDigits = true &#9;&#9;&#9;&#9;let mf = MeasurementFormatter() &#9;&#9;&#9;&#9;mf.numberFormatter = nf &#9;&#9;&#9;&#9;mf.unitOptions = .providedUnit &#9;&#9;&#9;&#9;return mf &#9;&#9;}() &#9;&#9; &#9;&#9;//... &#9;&#9;var after: String { &#9;&#9;&#9;&#9;let input: Measurement<UnitPower> &#9;&#9;&#9;&#9;let output: String &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;switch inputUnits[inputUnitValue] { &#9;&#9;&#9;&#9;case "watts": input = Measurement(value: Double(inputValue) ?? 0, unit: .watts) &#9;&#9;&#9;&#9;case "femtowatts": input = Measurement(value: Double(inputValue) ?? 0, unit: .femtowatts) &#9;&#9;&#9;&#9;default: input = Measurement(value: Double(inputValue) ?? 0, unit: .watts) &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;switch outputUnits[outputUnitValue] { &#9;&#9;&#9;&#9;case "watts": output =&#9;outputFormatter.string(from: input.converted(to: .watts)) &#9;&#9;&#9;&#9;case "femtowatts": output = outputFormatter.string(from: input.converted(to: .femtowatts)) &#9;&#9;&#9;&#9;default: output = outputFormatter.string(from:&#9;input.converted(to: .watts)) &#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;return output &#9;&#9;} &#9;&#9;//... }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20
Reply to List and MysqlDB
I have a HomeUser to which I apply an "if" ... "if email == email@email.it" then I call BarberView passing as argument "f" ... BarberView will have to show me a List containing the lines of the result of the query shown above Thanks for your reply, and sorry I was not clear enough. Can you show the code you call BarberView passing as argument "f"? Especially, please include enough code to clarify what is passed to f, it should be an @State var, and whether it is changed or not while BarberView is shown.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20
Reply to How to pass data from a TextField to a List (SwiftUI)
Sorry for the late reply. Thanks for clarifying and sorry for missing the reply. Most parts get cleared, but it seems you have touched your code since then. There's another bug happening and I do not understand what this means or why you do not want it. I figured out a way to show the cardInfo data in the list, but it's not the way I want it I think it's time for you to start your new thread to solve such another thing. Please do not forget to include all the latest code when starting a new thread.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20
Reply to What is _VectorMath protocol? Conforming to VectorArithmetic and AdditiveArithmetic
Proposed solution Thanks for showing your solution. And that is the same as what I tried. (Seems you already use Element.magnitudeSquared when implementing Array.magnitudeSquared. Follow-up on questions and remarks Thanks! I see them as coefficients of polynomials (as in the algebraic definition). If I am not mistaken, it can be a vector space. Coefficient space can make a vector space, but I'm not sure if it is a good example of VectorArithmetic. As VectorArithmetic is not just a linear space, but also provides magnitudeSquared. But, anyway, your description of + seems to be consistent. I did not see this sooner, but if I option-click on the _VectorMath, there's the quick help: Thanks, frankly, I was missing it.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20
Reply to I have a function in one view, but a button that should call it in another
One way to solve your issue is creating a sharable object and move onAdd into the object. You can extend CardsInfo. Something like this: class CardsInfo: ObservableObject { &#9;&#9;@Published var newCard: CardInfo = CardInfo() &#9;&#9;@Published var cards: [Card] = [] //<- &#9;&#9; &#9;&#9;//↓ Move `onAdd` here (and renamed) &#9;&#9;func add() { &#9;&#9;&#9;&#9;cards.append(Card(title: "Card #\(cards.count)")) &#9;&#9;} } Your AddView would look like this sharing CardsInfo: struct AddView: View { &#9;&#9;@ObservedObject var cardsInfo: CardsInfo //<- Hold shared object here &#9;&#9; &#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;CardView(name: cardsInfo.newCard.name, id: cardsInfo.newCard.id) //<- &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;TextField("Name", text: $cardsInfo.newCard.name) //<- &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.textFieldStyle(RoundedBorderTextFieldStyle()) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.shadow(radius: 10) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;TextField("ID", text: $cardsInfo.newCard.id) //<- &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.textFieldStyle(RoundedBorderTextFieldStyle()) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.shadow(radius: 10) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;TextField("Card Name", text: $cardsInfo.newCard.cname) //<- &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.textFieldStyle(RoundedBorderTextFieldStyle()) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.shadow(radius: 10) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Button(action: { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;cardsInfo.add() //<- You can call methods in a shared object &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;}) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text("Create") &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.bold() &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.disabled(self.cardsInfo.newCard.name.isEmpty self.cardsInfo.newCard.id.isEmpty &#9;&#9;self.cardsInfo.newCard.cname.isEmpty) //<- &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.foregroundColor(.white) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.padding() &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.padding(.horizontal, 100) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.background(Color.accentColor) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.cornerRadius(10) &#9;&#9;&#9;&#9;&#9;&#9;}.padding() &#9;&#9;&#9;&#9;&#9;&#9;.navigationTitle(cardsInfo.newCard.cname) //<- &#9;&#9;&#9;&#9;&#9;&#9;.navigationBarTitleDisplayMode(.inline) &#9;&#9;&#9;&#9;} &#9;&#9;} } And your CardsView as: struct CardsView: View { &#9;&#9;@StateObject var cardsInfo = CardsInfo() //<- Share this object &#9;&#9;@State var showSheetView = false &#9;&#9;@State private var editMode = EditMode.inactive &#9;&#9;//↓ Move `cards` into `CardsInfo` &#9;&#9;//@State private var cards: [Card] = [] &#9;&#9;//↓ You have no need to have `count` where you can easily access `cards.count` &#9;&#9;&#9;&#9;//private static var count = 0 &#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;ForEach(cardsInfo.cards) { card in &#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;} &#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;.navigationTitle("Cards") &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.toolbar { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;ToolbarItem(placement: .navigationBarLeading) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;EditButton() &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;ToolbarItem(placement: .navigationBarTrailing) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Button(action: { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.showSheetView.toggle() &#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;Image(systemName: "plus") &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;}.sheet(isPresented: $showSheetView) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;AddView(cardsInfo: cardsInfo) //<- Pass the shared object &#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;&#9;&#9;&#9;&#9;.environment(\.editMode, $editMode) &#9;&#9;&#9;&#9;} &#9;&#9;} &#9;&#9;//↓ Remove this method //&#9;&#9;private var addButton: some View { //&#9;&#9;&#9;&#9;switch editMode { //&#9;&#9;&#9;&#9;case .inactive: //&#9;&#9;&#9;&#9;&#9;&#9;return AnyView(Button(action: onAdd) { Image(systemName: "plus") }) //&#9;&#9;&#9;&#9;default: //&#9;&#9;&#9;&#9;&#9;&#9;return AnyView(EmptyView()) //&#9;&#9;&#9;&#9;} //&#9;&#9;} &#9;&#9;//↓ Move this into the shared object //&#9;&#9;func onAdd() { //&#9;&#9;&#9;&#9;withAnimation { //&#9;&#9;&#9;&#9;&#9;&#9;cards.append(Card(title: "Card #\(Self.count)")) //&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Self.count += 1 //&#9;&#9;&#9;&#9;} //&#9;&#9;} &#9; &#9;&#9;private func onDelete(offsets: IndexSet) { &#9;&#9;&#9;&#9;cardsInfo.cards.remove(atOffsets: offsets) //<- &#9;&#9;} &#9;&#9; &#9;&#9;private func onMove(source: IndexSet, destination: Int) { &#9;&#9;&#9;&#9;cardsInfo.cards.move(fromOffsets: source, toOffset: destination) //<- &#9;&#9;} } There may be many parts to fix to make this work, but please try.
Topic: App & System Services SubTopic: General Tags:
Dec ’20
Reply to Swift UI runtime order
The trailing comma is making the URL hard to follow. Building Lists and Navigation - https://developer.apple.com/tutorials/swiftui/building-lists-and-navigation Generally, it is very hard to specify when some executable statements in a SwiftUI app will be executed. One reason is that many parts of SwiftUI are not clearly documented, another is many parts will be executed on demand. when does load get called in that sequence. So, focus on this most specific case. In this case, load(_:) is used as the initial value in the definition of global variable landmarks. Global variables are lazily initialized in Swift, meaning at the first place it is accessed. So, you may need to find where it is accessed first. You can find some places using landmarks, but I think it is line 10 of LandmarkList.swift: /* See LICENSE folder for this sample’s licensing information. Abstract: A view showing a list of landmarks./ import SwiftUI struct LandmarkList: View { &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;NavigationView { &#9;&#9;&#9;&#9;&#9;&#9;List(landmarks) { landmark in //<- The first usage of `landmarks` &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;NavigationLink(destination: LandmarkDetail(landmark: landmark)) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;LandmarkRow(landmark: landmark) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;.navigationTitle("Landmarks") &#9;&#9;&#9;&#9;} &#9;&#9;} } So, when the body of LandmarkList is evaluated first, landmarks is accessed and load(_:) is called. Is this sufficient for you? Whether your answer is yes or no, we cannot go deeper than this, as SwiftUI does not specify when body is evaluated. One thing sure is that body is evaluated before the view is shown. Thus, load(_:) is called sometime before LandmarkList is shown. That's all we can say about when.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20
Reply to List and MysqlDB
then call barberview passing f as parameter. Thanks for showing your code. Seems your BarberView is only presented as a destination of NavigationLink. Please try the following. First, add an initializer which does not call load(f:) to MovieFetcher: public class MovieFetcher: ObservableObject { &#9;&#9;@Published var movies = [Movie]() &#9;&#9;//↓ Add an initializer which does not call `load(f:)` &#9;&#9;init() {} &#9;&#9; &#9;&#9;//&#9;&#9;@Published var f:String &#9;&#9;// &#9;&#9;// &#9;&#9;//&#9;&#9;init(f: String){ &#9;&#9;//&#9;&#9;&#9;&#9;self.f = f &#9;&#9;//&#9;&#9;&#9;&#9;load(f: f) &#9;&#9;//&#9;&#9;} &#9;&#9; &#9;&#9;//... } And then change your BarberView to use it, and call load(f:) explicitly: struct BarberView: View { &#9;&#9;@Binding var f:String &#9;&#9; &#9;&#9; &#9;&#9;@StateObject var fetcher = MovieFetcher() //<- &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;VStack { &#9;&#9;&#9;&#9;&#9;&#9;//... &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;//↓ Call `load(f:)` explicitly &#9;&#9;&#9;&#9;.onAppear { &#9;&#9;&#9;&#9;&#9;&#9;fetcher.load(f: f) &#9;&#9;&#9;&#9;} &#9;&#9;} } Assuming MovieFetcher works as expected.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’21
Reply to Compatible teaching book with Xcode
Is this the book you are using? Develop in Swift Fundamentals - https://books.apple.com/us/book/develop-in-swift-fundamentals/id1511184145 For example, on page 79, the instructions are to “In ViewController.swift, add the following lines of code below super.viewDidLoad():" But super.viewDidLoad () isn't to be found in the contents area. I'm afraid you are mistaking something. I do not understand what you mean by the contents area, but super.viewDidLoad() in the instruction is not a thing you find in Xcode, but it is you that type in as to make super.viewDidLoad() in the editor page. I started with the latest version of Xcode, 12.7. Then I noticed that the book was using a version 11 of Xcode, so I tried versions 11.7 and 11.2.1, but they weren't compatible either. There is no Xcode of version 12.7. I think minor version may not affect significantly, but you should better check the right version when you talk about version compatibilities.
Jan ’21