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 RxSwift | |
import SocketIO | |
extension Reactive where Base: SocketIOClient { | |
public func on(_ event: String) -> Observable<[Any]> { | |
return Observable.create { observer in | |
let id = self.base.on(event) { items, _ in | |
observer.onNext(items) | |
} |
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 RxSwift | |
extension ObservableType { | |
/// A map that unwraps an optional value, only continuing if the result is not nil. | |
func ignoreNilMap<R>(transform: @escaping (E) -> R?) -> Observable<R> { | |
return Observable.create { observer in | |
return self.subscribe { element in | |
switch element { | |
case .next(let value): |
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 Foundation | |
let progress = Progress(parent: nil, userInfo: [ | |
.fileOperationKindKey: Progress.FileOperationKind.downloading, | |
.fileURLKey: URL(fileURLWithPath: "/Users/mminer/Downloads/somefile.zip"), | |
]) | |
progress.isCancellable = true | |
progress.isPausable = false | |
progress.kind = .file |
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 Alamofire | |
import RxSwift | |
extension Request: ReactiveCompatible {} | |
extension Reactive where Base: DataRequest { | |
func responseJSON() -> Observable<Any> { | |
return Observable.create { observer in | |
let request = self.base.responseJSON { response in |
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 Foundation | |
import IOKit | |
func getMACAddress() -> String { | |
let matching = IOServiceMatching("IOEthernetInterface") as NSMutableDictionary | |
matching[kIOPropertyMatchKey] = ["IOPrimaryInterface": true] | |
var servicesIterator: io_iterator_t = 0 | |
defer { IOObjectRelease(servicesIterator) } | |
guard IOServiceGetMatchingServices(kIOMasterPortDefault, matching, &servicesIterator) == KERN_SUCCESS else { |
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 Foundation | |
extension String { | |
/// Finds and returns the ranges of occurrences of a given string within a given range of the `String`. | |
func ranges(of searchString: String, options: CompareOptions = [], range searchRange: Range<Index>? = nil, locale: Locale? = nil) -> [Range<Index>] { | |
let searchRange = searchRange ?? startIndex..<endIndex | |
if let foundRange = range(of: searchString, options: options, range: searchRange, locale: locale) { | |
let nextRange = foundRange.upperBound..<searchRange.upperBound |
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 Collection { | |
/// Returns the element at the specified index if it is within bounds, or nil if it's outside. | |
subscript(safe index: Index) -> Iterator.Element? { | |
return indices.contains(index) ? self[index] : nil | |
} | |
} |
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
// You don't necessarily need this subclass if your NSTextView is selectable. | |
// If it isn't though, this allows you to have an uneditable, unselectable label where links work as expected. | |
import AppKit | |
class HyperlinkTextView: NSTextView { | |
override func mouseDown(with event: NSEvent) { | |
super.mouseDown(with: event) | |
openClickedHyperlink(with: event) |
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 Cocoa | |
extension NSMutableAttributedString { | |
/// Makes the specified text a clickable hyperlink. | |
func hyperlink(text: String, with url: URL, additionalAttributes: [NSAttributedStringKey: Any] = [:]) { | |
var attributes = additionalAttributes | |
attributes[.link] = url.absoluteString | |
let range = (string as NSString).range(of: text) | |
addAttributes(attributes, range: range) |
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 AppKit | |
extension NSApplication { | |
/// Restarts the application. | |
func restart() { | |
let process = Process() | |
process.launchPath = "/bin/sh" | |
process.arguments = ["-c", "sleep 1; open '\(Bundle.main.bundlePath)'"] | |
process.launch() |