Skip to content

Instantly share code, notes, and snippets.

View swiftcode's full-sized avatar

mpc swiftcode

  • 05:12 (UTC -05:00)
View GitHub Profile
@swiftcode
swiftcode / promoCode.swift
Last active November 12, 2024 14:18
Promo Code Generator
//Quick five minute snippet to generate a promo code based on a given format.
//C = Character, N = Number, A = Alphanumeric.
//When I get some time, I'll put more thought into making a better algorithm
func generatePromoCode(using format: String) -> String {
var returnCode: String = ""
let alphabet = (65...90).map({Character(UnicodeScalar($0))})
let numbers = (48...57).map({Character(UnicodeScalar($0))})
let alphanumeric = alphabet + numbers
@swiftcode
swiftcode / ObservableObject.swift
Last active October 17, 2024 01:23
ObservableObject
final class ObservableObject<T> {
var value: T {
didSet {
listener?(value)
}
}
private var listener: ((T) -> Void)?
init(_ value: T) {
@swiftcode
swiftcode / devicetype.swift
Created September 7, 2024 14:47
iOS Device Type
//Taken from StackOverflow. I've not tested this thoroughly. Check on actual devices before using in production code
func modelIdentifier() -> String {
if let simulatorModelIdentifier = ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] { return simulatorModelIdentifier }
var sysinfo = utsname()
uname(&sysinfo) // ignore return value
return String(bytes: Data(bytes: &sysinfo.machine, count: Int(_SYS_NAMELEN)), encoding: .ascii)!.trimmingCharacters(in: .controlCharacters)
}
@swiftcode
swiftcode / battery.swift
Last active September 7, 2024 14:27
Battery Check
var connected: Bool {
return UIDevice.current.batteryState != .unplugged
}
var batteryLevel: Int {
UIDevice.current.isBatteryMonitoringEnabled = true
let level: Int = Int(UIDevice.current.batteryLevel * 100)
return level
}
@swiftcode
swiftcode / conversion.swift
Last active August 23, 2024 01:33
Temperature Conversion - SwiftUI
// **** NOTE ****
// It turns out that Apple has an API for this: UnitTemperature
// More information at: https://developer.apple.com/documentation/foundation/unittemperature
// Convert temperature to Fahrenheit, Celcius and Kelvin
// Note: This was quickly put together and verifying the results is warranted.
// FIXME: Kelvin converts to celcius, which will most assuredly produce incorrect
// results, as no attempt was made to determine what unit is coming in.
//Code comes from https://www.youtube.com/watch?v=I1imChxYClk&list=PLEVREFF3xBv4MviDGwlFz-onZ08TWrNjD
//Will need to make slight mods to make work with actual project (i.e. SimpleView_Preview needs renamed)
//PreviewViewController
#if canImport(SwiftUI) && DEBUG
import SwiftUI
struct UIViewControllerPreview<ViewController: UIViewController> : UIViewControllerRepresentable {
let viewController: ViewController
@swiftcode
swiftcode / spacer.sh
Created April 24, 2024 07:23
Add spacer in dock bar
defaults write com.apple.dock persistent-apps -array-add '{"tile-type"="spacer-tile";}'
killall Dock
@swiftcode
swiftcode / String+Array.swift
Created November 1, 2023 03:10
String+Array
extension Array {
func toString() -> String {
var result: String = ""
for element in self {
result = "\(result),\(element)"
}
return String(result.dropFirst())
}
@swiftcode
swiftcode / WebView.swift
Last active June 27, 2023 01:13
WebViewExample
import UIKit
import WebKit
class WebViewer: UIViewController, WKUIDelegate {
var webView = WKWebView()
var filename: String = ""
var fileUrl: URL?
override func loadView() {
import Cocoa
import Foundation
import WebKit
@main
class AppDelegate: NSObject, NSApplicationDelegate, WKNavigationDelegate {
let statusItem = NSStatusBar.system.statusItem(withLength:NSStatusItem.squareLength)
let popover = NSPopover()