How do you write a completion handler in Swift 3?

In Swift 3 the function parameter labels in closures are gone. Remove all occurrences of success: and add @escaping func Logout(completionHandler:@escaping (Bool) -> ()) { backendless?.userService.logout( { user in print(“User logged out.”) completionHandler(true) }, error: { fault in print(“Server reported an error: \(fault)”) completionHandler(false) }) } And use it Logout() { success in print(success) }

Array element cannot be bridged to Objective-C

The reason Objective-C is mentioned is because UIKit and QuartzCore are Objective-C frameworks. In particular, gradient.colors = arrayColors is calling an Objective-C method that expects an NSArray. This seems like a bug, as Apple’s documentation makes it sound like that the array should auto-bridge to an NSArray so long as the items in the array … Read more

Whats the Swift animate WithDuration syntax?

Swift 3/4 Syntax Here’s an update with the Swift 3 Syntax: UIView.animate(withDuration: 0.5, delay: 0.3, options: [.repeat, .curveEaseOut, .autoreverse], animations: { self.username.center.x += self.view.bounds.width }, completion: nil) If you need to add a completion handler just add a closure like so: UIView.animate(withDuration: 0.5, delay: 0.3, options: [.repeat, .curveEaseOut, .autoreverse], animations: { // animation stuff }, … Read more

What’s the difference between using ARAnchor to insert a node and directly insert a node?

Update: As of iOS 11.3 (aka “ARKit 1.5”), there is a difference between adding an ARAnchor to the session (and then associating SceneKit content with it through ARSCNViewDelegate callbacks) and just placing content in SceneKit space. When you add an anchor to the session, you’re telling ARKit that a certain point in world space is … Read more

Swift 4 Decodable – Dictionary with enum as key

The problem is that Dictionary‘s Codable conformance can currently only properly handle String and Int keys. For a dictionary with any other Key type (where that Key is Encodable/Decodable), it is encoded and decoded with an unkeyed container (JSON array) with alternating key values. Therefore when attempting to decode the JSON: {“dictionary”: {“enumValue”: “someString”}} into … Read more

How to configure ContextMenu buttons for delete and disabled in SwiftUI?

All of the asked situations are now supported in iOS 15 Destructive: (works from iOS 15) Set .destructive as the role argument of the button: Button(role: .destructive) { // 👈 This argument // delete something } label: { Label(“Delete”, systemImage: “trash”) } Disabled: (works from iOS 14.2) Add .disabled modifier to the button. Button { … Read more

Check if date falls between 2 dates

Swift ≧ 3 Swift 3 makes this a lot easier. let fallsBetween = (startDate … endDate).contains(Date()) Now that NSDate is bridged to the value type Date and Date conforms to Comparable we can just form a ClosedRange<Date> and use the contains method to see if the current date is included. Caveat: endDate must be greater … Read more

Get width of a view using in SwiftUI

The only available mechanism to get the dimension of a view, that is auto-resized by SwiftUI, is the GeometryReader. The GeometryReader is a proxy view that returns the dimensions of the container in which your view gets rendered. struct SomeView: View { @State var size: CGSize = .zero var body: some View { VStack { … Read more