How to generate a random number in Swift?

Swift 4.2+ Swift 4.2 shipped with Xcode 10 introduces new easy-to-use random functions for many data types. You can call the random() method on numeric types. let randomInt = Int.random(in: 0..<6) let randomDouble = Double.random(in: 2.71828…3.14159) let randomBool = Bool.random()

Can you execute an Applescript script from a Swift Application

As Kamaros suggests, you can call NSApplescript directly without having to launch a separate process via NSTask (as CRGreen suggests.) Swift Code let myAppleScript = “…” var error: NSDictionary? if let scriptObject = NSAppleScript(source: myAppleScript) { if let output: NSAppleEventDescriptor = scriptObject.executeAndReturnError( &error) { print(output.stringValue) } else if (error != nil) { print(“error: \(error)”) } … Read more

SwiftUI – State change in UITextField causes the frameWidth to grow uncontrolled and ignore bounds/frame

You need to decrease priority of content resistance in makeUIView (so content would not push external layout set in SwiftUI) like below func makeUIView(context: UIViewRepresentableContext<MyField>) -> UITextField { let field = UITextField(frame: .zero) field.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) …

How do I declare a variable that has a type and implements a protocol?

I think you can get there by adding an (empty) extension to UIViewController and then specifying your detailViewController attribute using a composed protocol of the empty extension and your DetailViewController. Like this: protocol UIViewControllerInject {} extension UIViewController : UIViewControllerInject {} Now all subclasses of UIViewController satisfy protocol UIViewControllerInject. Then with that, simply: typealias DetailViewControllerComposed = … Read more