Post

Replies

Boosts

Views

Activity

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 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 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 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 Cannot find "self" in scope & Consecutive declarations on a line must be separated by ";".
Why is this happening? Because you are using self in the initial values of instance property declaration. And you are writing executable statements where they are not allowed. why self cannot be found in scope?  You cannot use self in the initial values of instance property declaration in Swift. Isn't SELF supposed to be pointing to GestureManager class? Swift utilizes case-sensitive identifiers. You should better care about letter cases. If you want some words to be emphasized, you can use some markdown features. But the most critically bad thins is this one:  I'm expecting to pass the ViewController to be used in the "target" attribute When you use target-action pattern, the target and the action needs to be consistent. If you pass self.vc to target:, you need to pass an instance method of self.vc to action:. The right fix may depend on the usage of the class GestureManager, but one possible fix would be something like this: class GestureManager { var vc: UIViewController? @objc func respondToSwipeGesture(_ gesture : UIGestureRecognizer) { if let swipeGesture = gesture as? UISwipeGestureRecognizer { switch swipeGesture.direction { case .right: print("swiped right!") case .left: print("Swiped left") case .up: print("Swiped up") case .down: print("Swiped down") default: return } } } lazy var rightSwipe: UISwipeGestureRecognizer = { let rightSwipeGR = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(_:))) rightSwipeGR.direction = .right return rightSwipeGR }() lazy var leftSwipe: UISwipeGestureRecognizer = { let leftSwipeGR = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(_:))) leftSwipeGR.direction = .left return leftSwipeGR }() } You can use self in the initial values of lazy instance property. To run some executable statement like rightSwipe.direction = .right, you need to create a function and call it. If you do want to call the method respondToSwipeGesture(_:) of GestureManager, the target needs to be self, not self.vc. If you are not accustomed to utilizing closure, you can define explicit functions: lazy var rightSwipe: UISwipeGestureRecognizer = createRightSwipeGR() private func createRightSwipeGR() - UISwipeGestureRecognizer { let rightSwipeGR = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(_:))) rightSwipeGR.direction = .right return rightSwipeGR } lazy var leftSwipe: UISwipeGestureRecognizer = createLeftSwipeGR() private func createLeftSwipeGR() - UISwipeGestureRecognizer { let leftSwipeGR = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(_:))) leftSwipeGR.direction = .left return leftSwipeGR }
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21
Reply to What's wrong with this code? Swipe Gesture not working
it just does not work. You should better clarify what you mean by does not work. You mean any print statements in respondToSwipeGesture(_:) does not output anything? The worst thing in your code is that you are using lazy for the property gestureManager but you are not using gestureManager anywhere in your code. Try something like this: class ViewController: UIViewController { fileprivate lazy var gestureManager: GestureManager = { return GestureManager(vc: self) }() override func viewDidLoad() { super.viewDidLoad() print(gestureManager) //- } } But I would use non-lazy in this case: class ViewController1: UIViewController { fileprivate var gestureManager: GestureManager! override func viewDidLoad() { super.viewDidLoad() gestureManager = GestureManager(vc: self) } } With this change, you would find something happens, but it may not be what you want. As I wrote in another thread of yours - https://developer.apple.com/forums/thread/678795, your GestureManager has some flaws and you have not fixed them all.
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21
Reply to Is there any technical reason why Xcode 12.5 cant be installed in Catalina?
Is there any technical reason why Xcode 12.5 cant be installed in Catalina? Apple has never revealed what sort of technical reasons there were when putting some requirements on macOS for new versions of Xcode. Xcode Support page  - https://developer.apple.com/support/xcode We just need to fulfill the requirements provided by Apple if we want to develop apps for the platform founded by Apple. You can write a feature request to Apple that Xcode should support wider range of macOS versions using the Apple's Feedback Assistant.
Apr ’21
Reply to Getting CPU type in a Swiftly manner for macOS
natively in swift without having to call a system process? I do not know any Swift classes which returns the exact result as uname -m. One possible way is calling the system function in Swift: var systeminfo = utsname() uname(&systeminfo) let machine = withUnsafeBytes(of: &systeminfo.machine) {bufPtr-String in let data = Data(bufPtr) if let lastIndex = data.lastIndex(where: {$0 != 0}) { return String(data: data[0...lastIndex], encoding: .isoLatin1)! } else { return String(data: data, encoding: .isoLatin1)! } } print(machine)
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21
Reply to NSSavePanel Error
The app's entitlements does NOT have "com.apple.security.files.user-selected.read-write". How do you add this? Assuming your app is a sandboxed macOS app. Open the project file and choose the Signing & Capabilities tab of the target. You can find the User Selected File in File Access Type of App Sandbox. Check the value is set to Read/Write. The entitlement is automatically managed by this setting.
Topic: UI Frameworks SubTopic: AppKit Tags:
Apr ’21
Reply to Networking error management
Also, why can't I implement throwsin my function when it is throwing ? If you declare your connect as throws, it should: return normally return with error (when error thrown) The completion handler of dataTask is executed after connect returned. This is why I would like to implement a throw scenario to my request If you are having such use case in mind, completion handler pattern might be easier: &#9;&#9;func connect(url: String, &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; onError errorHandler: @escaping (Error?)->Void) { //<- &#9;&#9;&#9;&#9;let encoded: Data &#9;&#9;&#9;&#9;do { &#9;&#9;&#9;&#9;&#9;&#9;encoded = try JSONEncoder().encode(connexion) &#9;&#9;&#9;&#9;} catch { &#9;&#9;&#9;&#9;&#9;&#9;print("Fail to encode newMed: \(error)") &#9;&#9;&#9;&#9;&#9;&#9;return &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;let url = URL(string: "/api/\(url)")! &#9;&#9;&#9;&#9;var request = URLRequest(url: url) &#9;&#9;&#9;&#9;request.setValue("application/json", forHTTPHeaderField: "Content-Type") &#9;&#9;&#9;&#9;request.httpMethod = "POST" &#9;&#9;&#9;&#9;request.httpBody = encoded &#9;&#9;&#9;&#9;URLSession.shared.dataTask(with: url) { data, res, error in &#9;&#9;&#9;&#9;&#9;&#9;if let error = error { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;errorHandler(error) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;return &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;guard let response = res else { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;errorHandler(ConnexionError.invalidServerRes) //<- &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;return &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;guard let httpResponse = response as? HTTPURLResponse, &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;case 200...299 = httpResponse.statusCode &#9;&#9;&#9;&#9;&#9;&#9;else { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.handleServerError(res!) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;return &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;guard let data = data else { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;print("data is nil") &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;return &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;let decoder = JSONDecoder() &#9;&#9;&#9;&#9;&#9;&#9;do { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;let json = try decoder.decode(Connexion.self, from: data) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;print(json) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.connexion.id = json.id &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.connexion.token = json.token &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.connexion.sessionID = json.sessionID &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.signInSuccess = true &#9;&#9;&#9;&#9;&#9;&#9;} catch { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;let dataString = String(data: data, encoding: .utf8) ?? "Unknown encoding" &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;print("Invalid response \(error) - \(dataString)") &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;}.resume() &#9;&#9;} And use it as: &#9;&#9;&#9;&#9;Button(action: { &#9;&#9;&#9;&#9;&#9;&#9;self.showGroup = false &#9;&#9;&#9;&#9;&#9;&#9;if connexion.isNotEmpty { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.connect(url: "url", onError: { err in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.showError = true &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;}) &#9;&#9;&#9;&#9;&#9;&#9;} else { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.showEmpty = true &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;}) { &#9;&#9;&#9;&#9;&#9;&#9;Text("button") &#9;&#9;&#9;&#9;} Settings :  Sorry, far from I expected. Please show all the customizable things of Postman, including Http Method, Authorization, Header and Body.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21
Reply to Swift execute code after return, unbelievable!!
Compiling the shown code, Xcode showed this warning: Expression following 'return' is treated as an argument of the 'return' The two line is treated as: &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;return print("what?") In Swift, return statement in a Void function can return only one specific value Void(). (An instance of Void, which is equivalent as the empty tuple ().) And the return type of print is Void, which means it returns Void(). Thus, the result of calling print("what?") can be a return value of Void function as you see.
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21