Skip to content

Instantly share code, notes, and snippets.

View popmedic's full-sized avatar

Popmedic popmedic

View GitHub Profile
@popmedic
popmedic / resolve.swift
Last active July 12, 2021 14:54
Resolve a host for v4 or v6 in Swift
#if os(OSX)
import Cocoa
#else
import Foundation
#endif
/**
resolves a host names ip address for the specified family
- parameters:
- name: the host name to resolve
@popmedic
popmedic / ExternIP.swift
Last active June 30, 2021 20:02
A quick example of how to get the External IP address of a device that is on a LAN connected by an Access Point to the WAN.
import SwiftUI
import Network
class ExternIP: ObservableObject {
@Published var address: String = "unknown"
@Published var isV6Supported: Bool = false
init() {
queue = DispatchQueue(label: "monitor")
monitor = NWPathMonitor()
@popmedic
popmedic / string+escaping.swift
Created March 26, 2021 14:15
String Escape and Unescape
import Cocoa
let given = "{\n\t\"test\": \"this 😃 \\ \",\n\r\t\"\'testing\': 1\r\n}\\ \0 \\"
let expected = #"{\n\t\"test\": \"this 😃 \\ \",\n\r\t\"\'testing\': 1\r\n}\\ \0 \\"#
let expectedAscii = #"{\n\t\"test\": \"this \u{0001F603} \\ \",\n\r\t\"\'testing\': 1\r\n}\\ \0 \\"#
extension String {
private static let escapedChars = [
(#"\0"#, "\0"),
(#"\t"#, "\t"),
(#"\n"#, "\n"),
@popmedic
popmedic / swift-httprequesting-coverage.svg
Last active March 4, 2021 17:08
swift httprequesting coverage svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@popmedic
popmedic / plistVjsonToDict.swift
Last active April 25, 2020 17:40
Plist vs. JSON for converting Encodable implementation to Any (Dictionary)
import Foundation
struct LotsOfStuff: Codable {
let x: Int
let y: Int
let z: Double
let a: [String]
let b: String
let c: String
let less: LessStuff
import UIKit
protocol Invokable {
var order: Int { get }
func invoke()
}
class A: Invokable {
var order = 0
func invoke() { print("A") }
import UIKit
/**
Class to make "Toast," like in Android, for iOS in Swift 4.x.
example:
```
Toast.backgroundColor = UIColor.init(red: 0.0, green: 1.0, blue: 0.0, alpha: 0.6)
Toast.textColor = UIColor.init(red: 1.0, green: 0.0, blue: 0.0, alpha: 0.6)
Toast.textAlignment = .center
Toast.font = UIFont.systemFont(ofSize: 14.0)
@popmedic
popmedic / Extension.swift
Last active July 13, 2018 14:58
(Swift 4.x) An extension to UIImage to load from a remote URL and an extension to UIImageView to load its image using the UIImage extension.
import UIKit
extension UIImage {
/// will load an image from a remote URL
/// - parameter fromURL: url to load from
/// - parameter completionHandler: completion handler for when the image is done loading.
/// - parameter image: UIImage? that was retrieved, or nil on error
/// - parameter error: LocalizedError? that is nil on success, or an error message on failure.
/// example
/// ```
@popmedic
popmedic / tldlist.swift
Last active March 7, 2021 10:26
a class with a static member with all top-level-domains
class TLDList {
static let tlds = [
"*.aaa",
"*.aarp",
"*.abarth",
"*.abb",
"*.abbott",
"*.abbvie",
"*.abc",
"*.able",