Skip to content

Instantly share code, notes, and snippets.

View mminer's full-sized avatar

Matthew Miner mminer

View GitHub Profile
@mminer
mminer / SocketIO+Rx.swift
Created December 2, 2016 20:27
SocketIOClient extension to subscribe to events via RxSwift observable.
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)
}
@mminer
mminer / ObservableType+IgnoreNilMap.swift
Created December 5, 2016 21:30
Custom RxSwift operator that maps and unwraps an optional value.
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):
@mminer
mminer / DownloadProgressIndicatorDemo.swift
Last active November 3, 2023 08:44
Displays a progress indicator on a file in the Finder.
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
@mminer
mminer / Alamofire+Rx.swift
Created January 6, 2017 21:11
Example of Alamofire RxSwift response serialization extension.
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
@mminer
mminer / getMACAddress.swift
Created February 1, 2017 19:13
Finds the machine's MAC address.
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 {
@mminer
mminer / String+Ranges.swift
Created March 1, 2017 00:52
String extension to find the ranges of occurrences of a given string.
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
@mminer
mminer / Collection+SafeAccess.swift
Last active October 18, 2017 04:37
Collections extension for avoiding out-of-bounds exceptions.
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
}
}
@mminer
mminer / HyperlinkTextView.swift
Last active February 7, 2023 07:14
NSTextView subclass that displays and opens hyperlinks.
// 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)
@mminer
mminer / NSMutableAttributedString+Hyperlink.swift
Last active October 17, 2017 21:16
Extension to NSMutableAttributedString that creates a link.
@mminer
mminer / NSApplication+Restart.swift
Created May 2, 2017 19:49
Extension function to restart a Cocoa application.
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()