Post

Replies

Boosts

Views

Activity

Reply to How this code works? ViewController inherits from another ViewController it's viewDidLoad func
Just edited the post to change that.  Thanks for editing. Seems you know how to write replies. You should better respond when you get answers or comments on your posts. when ViewController1 gets pushed onto the stack, it would pretty much be calling two viewDidLoad methods, one from ViewController1 and the other one from ViewController2. Am I right? Your description is a little bit too difficult for me to understand, but I guess the answer is NO. When the view of ViewController1 is loaded the method viewDidLoad() of ViewController1 is called. In your case, you do not define (override) viewDidLoad() in ViewController1, so viewDidLoad() of ViewController2 is inherited to ViewController1 and the inherited viewDidLoad() is called when its view is loaded. (May not when the view controller is pushed.) So, if you need to implement viewDidLoad() for ViewController1 and still want the effect done by viewDidLoad() of ViewController2, you may need to use super. class ViewController1: ViewController2 { //... override func viewDidLoad() { super.viewDidLoad() //- Calls `viewDidLoad()` of `ViewController2` //... } //... }
Topic: UI Frameworks SubTopic: UIKit Tags:
Apr ’21
Reply to property initializers run before 'self' is available
As you see, in the struct ContentView, you have a declaration of an instance property named astronautsJson, and its initial value (in other words, property initializer) is decode("astronauts.json"). can someone tell me y it happens and how to fix this? As the error message is clearly stating, In Swift, you cannot use instance members (neither methods nor properties) within property initializer. You are trying to use the instance member (in your case, method) decode(_:), so Swift compiler is telling you you cannot. Using an extension (of other type than ContentView) would be a preferable way, but there may be other ways. One way is making your decode a type member (static method): struct ContentView: View { let astronautsJson: [Astronaut] = decode("astronauts.json") var body: some View { Text("\(astronautsJson.count)") .padding() } static func decode(_ file: String) - [Astronaut] { guard let url = Bundle.main.url(forResource: file, withExtension: nil) else { fatalError("Failed to locate \(file) in bundle.") } guard let data = try? Data(contentsOf: url) else { fatalError("Failed to load \(file) from bundle.") } let decoder = JSONDecoder() guard let loaded = try? decoder.decode([Astronaut].self, from: data) else { fatalError("Failed to decode \(file) from bundle.") } return loaded } } By the way, using try? with guard-let is not recommended. You should better find a better video: static func decode(_ file: String) - [Astronaut] { guard let url = Bundle.main.url(forResource: file, withExtension: nil) else { fatalError("Failed to locate \(file) in bundle.") } let data: Data do { data = try Data(contentsOf: url) } catch { fatalError("Failed to load \(file) from bundle with error: \(error)") } let decoder = JSONDecoder() let loaded: [Astronaut] do { loaded = try decoder.decode([Astronaut].self, from: data) } catch { fatalError("Failed to decode \(file) from bundle with error: \(error)") } return loaded } Using do-catch with try (not try?) would make your code a little longer, but you can get more info in case of errors, which may help you finding what is causing the error.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21
Reply to property initializers run before 'self' is available
answers like "I guess you do not understand what is instance member." don't help. Then you should have clarified what you do not understand. When you write an extension of Bundle with the method decode(_:) as in the video, you can use Bundle.main.decode(...) within the property initializer of astronautsJson. In this case, decode(_:) is an instance method of Bundle, not of ContentView, so you can use it in the property initializers of instance properties of ContentView. But in your code, you declare an instance method decode(_:) of ContentView. You cannot use instance members within the property initializer. This is a restriction of Swift. Do you understand?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21
Reply to Scroll View's height take the device's height
OK, I have copied the xml code into the source of the Main.storyboard of the test project. Xcode said it fixed some parts, but I think I have found the cause of the issue. Simply saying, the constraints are sort of broken. When you put a single view as the only content view of a UIScrollView, you connect the four edges of the view to the Content Layout Guide of the UIScrollView, and some other things to define horizontal position, usually width. And the content of the view need to define the height. You have not added enough constraints to define the height of Description view. The two things making the scroll view content to a fixed position and you can move it only in the margins of bounce scrolling. Not sure what sort of constraints you were trying to add, so it's hard to tell how to fix. So this is just an example which I could make the scroll view scroll:     ∨□Scroll View       □Content Layout Guide       □Frame Layout Guide      ∨□View        >□Header        >□Information        ∨□Description          □Movie Description         >□Reviews         ∨□Constraints           □Movie Description.top = top + 10           □trailing = Movie Description.trailing + 20           □Movie Description.leading = leading + 10           □bottom = Reviews.bottom + 109.5           □Reviews.centerX = centerX           □Movie Description.top = top + 10           □Reviews.top = Movie Description.bottom + 15        >□Constraints       >□Constraints      ∨□Constraints        □View.leading = Content Layout Guide.leading        □View.bottom = Content Layout Guide.bottom        □View.width = width        □View.top = Content Layout Guide.top        □View.trailing = Content Layout Guide.trailing Hope you can understand this and make the right layout for you.
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21
Reply to Thread 1: breakpoint 1.1 2.1
How do I solve? Better clarify solve what? This is my code: Please use Code block feature of this site (icon ``) when showing your code. I cannot get any messages like Thread 1: breakpoint 1.1 1.2 using the exact code you have shown. So the problem might exist in somewhere else in your project. Are you sure you have not added any breakpoint? Have really connected IBOutlets and IBActions correctly? Better try again creating a brand-new project, and copy the code, editing the storyboard again.
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21
Reply to Networking error management
I'm having this first issue: It is clear. You cannot throw from inside the completion handler of dataTask. The closure is called by iOS and iOS would never handle errors. You usually use (your own) completion handler to tell error to the caller, but in your case, adding a new @Published property holding the error would be an alternative. when I print the dataString Please show the core part of the dataString. And please show the settings of Postman when you get the right response. I guess you are not sending the same request in your code.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21
Reply to healthkit query
You should better clarify that you are writing a native method interfaced to React Native. I really do not want to go deep into React Native, with 2 reasons: It is not a framework of Apple's. I do not like it You should better find an appropriate place to ask something about React Native. I just want to correct terrible mis-directions... which part of my code is in-correct Your code has two big flaws: JS.side and Swift code are inconsistent Your JS code calls updateStepsCount() with no arguments. But in your Swift code has a parameter before resolve. It represents that the JS side should pass one parameter. You call execute(query), but there is no code receive the result of query The result would never go into statisticsCollection without writing an explicit code to do so. But this problem should be resolved in another thread of yours. Issue #1 JS.side and Swift code are inconsistent I think you cannot create an instance of HKStatisticsCollection, which means you cannot pass a parameter to Swift method. Remove the first parameter of updateStepsCount: 		@objc 		func updateStepsCount(_	resolve: @escaping RCTPromiseResolveBlock, 													rejecter reject: @escaping RCTPromiseRejectBlock) { 				// Find the right code in another thread of yours... 		} Controller.m RCT_EXTERN_METHOD(updateStepsCount: (RCTPromiseResolveBlock)resolve 									rejecter: (RCTPromiseRejectBlock)reject)
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21
Reply to In App Settings
Can you explain the steps to reproduce the behavior which you think is weird: Steps to reproduce What you expect with the steps What you actually get with the steps By the way, I can find many weird codings in your code shown: Home ViewController: static var riddleNotification = Bool() (26) -- Why using static? Why aren't you specifying the initial value explicitly? center.requestAuthorization (44) -- Why the completion handler is empty? func riddleNotification() { (50) -- Why making it nested inside viewDidLoad()? And better avoid the same name as property. else { (126) ... } (154) -- The else block would never be executed. Riddles(_:) (160), RiddlesandAnswers(_:) (174) -- Why using Capitalized names? Settings ViewController: leading_Settings (10), trailing_Settings (12) -- Why snake case? if Home.riddleNotification == true { (28) ... } (40) -- Why don't you write it simply riddleSwitch.setOn(Home.riddleNotification, animated: true)? if riddleSwitch.isOn { (84) ... } (100) -- Why don't you write it simply: 						Home.riddleNotification = riddleSwitch.isOn 						UDM.shared.defaults.setValue(riddleSwitch.isOn, forKey: "riddleNotification") And why do you have Home.riddleNotification and UserDefaults separately if they need to be synchronized to the same value? Some of them are just sort of convention things and may not be affecting your weird behavior, so you have no need to answer to all of the questions, but your code looks really weird for me.
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21