Programmatically Screenshot | Swift 3, macOS

Yes its possible. This function takes all connected monitors screenshots and writes to specified path as jpg file. Generates file name as unix time stamp. func TakeScreensShots(folderName: String){ var displayCount: UInt32 = 0; var result = CGGetActiveDisplayList(0, nil, &displayCount) if (result != CGError.success) { print(“error: \(result)”) return } let allocated = Int(displayCount) let activeDisplays = … Read more

No Such Module ‘Parse’

I just had this problem, and I got it to work by doing this: I opened my Target > Build Settings > Search Paths > Framework Search Paths. I added two values: $(PROJECT_DIR) and $(inherited) I don’t know why these were empty in the first place, but there you have it.

Do capture lists of inner closures need to redeclare `self` as `weak` or `unowned`?

The [weak self] in anotherFunctionWithTrailingClosure is not needed. You can empirically test this: class Experiment { func someFunctionWithTrailingClosure(closure: @escaping () -> Void) { print(“starting”, #function) DispatchQueue.main.asyncAfter(deadline: .now() + 1) { closure() print(“finishing”, #function) } } func anotherFunctionWithTrailingClosure(closure: @escaping () -> Void) { print(“starting”, #function) DispatchQueue.main.asyncAfter(deadline: .now() + 1) { closure() print(“finishing”, #function) } } func … Read more

Use reserved keyword a enum case

From the Swift Language Guide (Naming Constants & Variables section) If you need to give a constant or variable the same name as a reserved Swift keyword, surround the keyword with back ticks (`) when using it as a name. However, avoid using keywords as names unless you have absolutely no choice. enum MyEnum { … Read more

How to convert a binary to decimal in Swift?

Update for Swift 2: All integer types have an public init?(_ text: String, radix: Int = default) method now, which converts a string to an integer according to a given base: let binary = “11001” if let number = Int(binary, radix: 2) { print(number) // Output: 25 } (Previous answer:) You can simply use the … 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

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