Post

Replies

Boosts

Views

Activity

Reply to SwiftUI: Question about SwiftUI's Lifecyle, I think… Object isn't created properly
What is the reason for the different behaviour depending on which view I come from? I cannot answer to your question directly. But one thing is sure: You should not rely on when initializers of Views are called. The runtime of SwiftUI may evaluate body and may call the initializer of a View at anytime it is needed. You need to accept this fact when you write code in SwiftUI.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’21
Reply to Help with documentation : example: DisclosureGroup
in the first part it says label, I dont understand why. In Swift, identifiers are case-sensitive, you should better care about that. I can put a string and it work,  I do not understand what you have tried, but you cannot put String as Label, as String does not conform to View. Please show by code what you have tried. if I put a label I get an error saying it requires that 'Label<Text, Image>' conform to 'StringProtocol' Again, I do not understand what you did. Please show by code what you have tried. Anyway, Label here is just a placeholder used as a generic parameter. It has nothing to do with SwiftUI.Label. for me it says that label conform to view... ? True, the type used as Label, needs to conform to View. is there a documentation to help me read documentation :-) Have you ever read the Swift book - https://docs.swift.org/swift-book/? If not yet, I recommend you to read the section of Generics - https://docs.swift.org/swift-book/LanguageGuide/Generics.html carefully.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’21
Reply to Why is a Class faster in SwiftUI than a Struct? (with example)
I made 3 videos Sorry, but it is useless that you reproduce the issue in your environment. I'm not saying the issue never happens, but saying that it happens only in your environment. If you could clarify your testing environment, someone who can prepare the similar environment would test your code. Anyway, I cannot reproduce the issue you have described with the code currently shown.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’21
Reply to Help with documentation : example: DisclosureGroup
I am currently doing the CP193P from Stanford with Paul Hegarty based on the first SwiftUI  Thanks for sharing your background. When you write a SwiftUI code, the details of generics are not needed. You can re-learn generics alongside with (or after) learning how to write SwiftUI apps. (But I recommend you to read the Swift book thoroughly before starting the first Swift project.) Good luck and happy coding.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’21
Reply to Having problems with a SwiftUI checkbox binding I'm implementing...
The problem seems to be quite clear, no? class TangleFilter: ObservableObject { &#9;&#9;@Published var tangleTypes: [TangleTypeFilter] = TangleType.filterArray } When TangleTypeFilter is a reference type, changing some property would not be considered as changing tangleTypes. Whether or not the property is @Published. Thus, updating isChecked would not update UI. One way of achieving what you want is to make your TangleTypeFilter a struct: struct ContentView: View { &#9;&#9;@State var isChecked: Bool = false &#9;&#9;@ObservedObject var filter = TangleFilter() &#9;&#9;@State var singleFilter: TangleTypeFilter = TangleTypeFilter(tangleType: .grid) &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;VStack{ &#9;&#9;&#9;&#9;&#9;&#9;List(filter.tangleTypes.indices, id: \.self) {tangleTypeIndex in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;HStack { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;CheckBox(isChecked: $filter.tangleTypes[tangleTypeIndex].isChecked) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text("checked? \(filter.tangleTypes[tangleTypeIndex].isChecked.description)") &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;CheckBox(isChecked: $singleFilter.isChecked) &#9;&#9;&#9;&#9;} &#9;&#9;} } struct CheckBox: View { &#9;&#9;@Binding var isChecked: Bool { &#9;&#9;&#9;&#9;didSet { &#9;&#9;&#9;&#9;&#9;&#9;print("setting isChecked: \(isChecked)") &#9;&#9;&#9;&#9;} &#9;&#9;} &#9;&#9;var imageName: String { &#9;&#9;&#9;&#9;return isChecked ? "checkmark.square" : "square" &#9;&#9;} &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;Button(action: { &#9;&#9;&#9;&#9;&#9;&#9;self.isChecked.toggle() &#9;&#9;&#9;&#9;}) { &#9;&#9;&#9;&#9;&#9;&#9;Image(systemName: self.imageName) &#9;&#9;&#9;&#9;} &#9;&#9;} } enum TangleType: String, Codable, CaseIterable { &#9;&#9;static let filterArray: [TangleTypeFilter] = { &#9;&#9;&#9;&#9;var result: [TangleTypeFilter] = [] &#9;&#9;&#9;&#9;for tangleType in TangleType.allCases { &#9;&#9;&#9;&#9;&#9;&#9;result.append(TangleTypeFilter(tangleType: tangleType)) &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;return result &#9;&#9;}() &#9;&#9;case grid &#9;&#9;case row } class TangleFilter: ObservableObject { &#9;&#9;@Published var tangleTypes: [TangleTypeFilter] = TangleType.filterArray } struct TangleTypeFilter { &#9;&#9;var tangleType: TangleType &#9;&#9;var isChecked: Bool &#9;&#9;init(tangleType: TangleType) { &#9;&#9;&#9;&#9;self.tangleType = tangleType &#9;&#9;&#9;&#9;self.isChecked = false &#9;&#9;} }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’21
Reply to Navigation Bar Back Button
When you show your code, please clarify where these lines exist -- method, class or some others. How can I hide the navigation bar when the user returns to the main page after clicking the back button? Please try this: class MainPageViewController: UIViewController { &#9;&#9;override func viewDidLoad() { &#9;&#9;&#9;&#9;super.viewDidLoad() &#9;&#9;&#9;&#9;self.navigationItem.backButtonTitle = "hohoho" &#9;&#9;} &#9;&#9;override func viewWillAppear(_ animated: Bool) { &#9;&#9;&#9;&#9;super.viewWillAppear(animated) &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;self.navigationController?.navigationBar.isHidden = true &#9;&#9;} }
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’21
Reply to Why is a Class faster in SwiftUI than a Struct? (with example)
Please run this fullscreen and let me know your thoughts after running all sliders for a while. I needed to modify some parts of your project and tested it on Catalina. But the difference was very small. Maybe Big Sur is required to reproduce your issue. Maybe some more other things. Expecting you resolve this issue soon and clarify what is causing the lag you experienced.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’21
Reply to Having problems with a SwiftUI checkbox binding I'm implementing...
This was definitely not obvious Seems obvious is sort of subjective and it is not for you. does this mean that updating a property in a value type that is stored in an array will replace the array element with a new copy of the value type.  Yes. Have you heard that Swift Array is a value type? initially: filter.tangleTypes = [0x123456, 0x123466] What do you mean? What are 0x123456...? Are you talking about the case of Array containing struct (value type)?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’21
Reply to Parse XML in SwiftUI
Question that I just realized needed answered: How would I call ArticlesParser and Article into ArticlesView and other related views (ArticleView, ArticleRow, etc.)? And I assume Data would be held in Article or ArticlesParser? Do you remember that I wrote: let's concentrate on this issue. Please do not ask too many things in a thread. You asked how to use XMLParser and I have shown the answer for it. One more. Please check another thread of yours, and see how data is used there...
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’21
Reply to (Expansion) Call a Class into views
Do you remember I proposed to create an ObservableObject CardsInfo, which has an @Published array and a method called loadCards(). You can do it in the same way: class ArticlesInfo: ObservableObject { &#9;&#9;@Published var articles: ... &#9;&#9;//... &#9;&#9;func loadArticles() { &#9;&#9;&#9;&#9;// Use `ArticlesParser` here &#9;&#9;&#9;&#9;//... &#9;&#9;} } You can use it as in your CardsView.
Topic: App & System Services SubTopic: General Tags:
Jan ’21
Reply to Cannot convert value of type 'Binding<String>.Type' to expected argument type 'Binding<String>' error in swift 5 Xcode 12.3
How can I solve this ? That depends on how you defined your ContentView. You can try something like this: struct ContentView_Previews: PreviewProvider { &#9;&#9;static var previews: some View { &#9;&#9;&#9;&#9;ContentView(text: .constant("test value")) // <- &#9;&#9;} } When your ContentView have an @Binding property named text of type String, you need to pass an instance of Binding<String>, not the type name of itself.
Topic: App & System Services SubTopic: Core OS Tags:
Jan ’21
Reply to Having problems with a SwiftUI checkbox binding I'm implementing...
For quite some time I've imagined Swift arrays more like NSArray You should better have in mind, that in Swift, Array and Dictionary are value types. The behavior of them are far different from NSArray and NSDictionary. 0x123456 was intended to be a memory location (address) Seems you are too experienced about the implementation details of runtime of other programming languages. When you want to discuss about value types, memory location cannot be a good metaphor. Conceptually, every variable of value type holds the copy of the value for its own. I was under the impression that a swift array containing structs would be represented as a list of memory locations (one per struct/item in the array) Please be free from the implementation details and concentrate on how value types should work. When a property of an element (value type) of an Array is modified, Swift treats as the Array itself is modified. That's the nature of value type in Swift.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’21