This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension URLResponse { | |
/// Returns casted `HTTPURLResponse` | |
var http: HTTPURLResponse? { | |
return self as? HTTPURLResponse | |
} | |
} | |
extension HTTPURLResponse { | |
/// Returns `true` if `statusCode` is in range 200...299. | |
/// Otherwise `false`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// taken 2018-03-19 from wikipedia. https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes | |
public enum Iso639_1: String { | |
case ab = "AB" | |
case aa = "AA" | |
case af = "AF" | |
case ak = "AK" | |
case sq = "SQ" | |
case am = "AM" | |
case ar = "AR" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Check whether active window is in fullscreen mode. | |
/// - Parameter processIdentifier: the pid given by "NSWorkspace.shared.frontmostApplication?.processIdentifier" | |
/// - Returns: true if fullscreen, false if windowed | |
func isFullScreen(processIdentifier: pid_t) -> Bool { | |
if let winArray: CFArray = CGWindowListCopyWindowInfo(.excludeDesktopElements, kCGNullWindowID) { | |
for i in 0..<CFArrayGetCount(winArray) { | |
if let window: [String : Any] = unsafeBitCast(CFArrayGetValueAtIndex(winArray, i), to: CFDictionary.self) as? [String : Any] { | |
if let pid = window["kCGWindowOwnerPID"] as? Int32 { | |
if pid == processIdentifier { | |
if let bounds: [String : Any] = window["kCGWindowBounds"] as? [String : Any], let boundsWidth = bounds["Width"] as? CGFloat, let boundsHeight = bounds["Height"] as? CGFloat { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension AnyView { | |
/// Return UIHostingController for a SwiftUI View. | |
/// - Usage: let vc = AnyView( YourSwiftUIViewHere() ).asUIHostingController() | |
/// - Created by Michel Storms. | |
/// - Returns: UIHostingController ready to use with UIKit. | |
func asUIHostingController() -> UIHostingController<AnyView> { | |
return UIHostingController(rootView: self) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIViewController { | |
var previousViewController: UIViewController? { | |
guard let navigationController = navigationController else { return nil } | |
let count = navigationController.viewControllers.count | |
return count < 2 ? nil : navigationController.viewControllers[count - 2] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension NSManagedObject { | |
/// Print content of object to console. | |
func printContent() { | |
print("\nNSManagedObjectID: \(self.objectID) - key/value dump:") | |
for key in self.entity.attributesByName.keys { | |
print("\(key): \(self.value(forKey: key) ?? "nil")") | |
} | |
print("\n") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
def createFolder(directory): | |
try: | |
if not os.path.exists(directory): | |
os.makedirs(directory) | |
except OSError: | |
print ('Error: Creating directory. ' + directory) | |