Skip to content

Instantly share code, notes, and snippets.

View markiv's full-sized avatar

Vikram Kriplaney markiv

View GitHub Profile
@markiv
markiv / String+EmojiFlag.swift
Created April 30, 2024 10:57
Swift extension that creates a Unicode flag emoji for the given ISO region/country code.
public extension String {
fileprivate static let unicodeFlagsBase = 0x1F1E6 - UInt32("A")
/// Creates a Unicode flag emoji for the given ISO region/country code.
///
/// String(flag: "CH") // returns "🇨🇭"
/// String(flag: "in") // returns "🇮🇳"
/// String(flag: "eu") // returns "🇪🇺"
/// String(flag: "42") // returns nil
///
//
// A simplified implementation of https://gist.github.com/sturdysturge/e5163a9e95826adbeff9824d5aa1d111
// Which has an associated article here: https://betterprogramming.pub/build-a-secure-swiftui-property-wrapper-for-the-keychain-e0f8e39d554b
// Requires the Keychain Access package: https://github.com/kishikawakatsumi/KeychainAccess
//
import SwiftUI
import KeychainAccess
@propertyWrapper
@markiv
markiv / String+EmojiFlag.swift
Last active January 17, 2022 19:43
String extension to create emoji country and regional flags
extension String {
var flag: String {
uppercased().unicodeScalars
.filter { (65...90).contains($0.value) }
.compactMap { UnicodeScalar(0x1F1E6 - 65 + $0.value) }
.map(String.init)
.joined()
}
}
struct KeychainItem {
// MARK: Nested Types
enum KeychainError: Error {
case noPassword
case unexpectedPasswordData
case unexpectedItemData
case unhandledError(status: OSStatus)
}
@Akhu
Akhu / .gitignore
Last active January 2, 2023 11:10
Xcode 12 + Swift UI Gitignore
# Created by https://www.toptal.com/developers/gitignore/api/swiftpackagemanager,swift,xcode,macos
# Edit at https://www.toptal.com/developers/gitignore?templates=swiftpackagemanager,swift,xcode,macos
### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride
@rmcdongit
rmcdongit / macOS_SytemPrefs.md
Last active June 14, 2024 06:30
Apple System Preferences URL Schemes

macOS 10.15 System Preference Panes

Below are a list of System Preference pane URLs and paths that can be accessed with scripting to assist users with enabling macOS security settings without having to walk them through launching System Preferences, finding panes, and scrolling to settings. Not all panes have an accessible anchor and some are OS specific.

To find the Pane ID of a specific pane, open the System Preferences app and select the desired Preference Pane. With the pane selected, open the ScriptEditor.app and run the following script to copy the current Pane ID to your clipboard and display any available anchors:

tell application "System Preferences"
	set CurrentPane to the id of the current pane
	set the clipboard to CurrentPane
@drumnkyle
drumnkyle / AllComponentPreviews.swift
Last active March 15, 2022 22:32
SwiftUI Preview Helpers for Most Edge Cases
/**
Copyright 2019 Kyle Sherman
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
@AliSoftware
AliSoftware / Demo.swift
Last active October 31, 2023 12:25
NestableCodingKey: Nice way to define nested coding keys for properties
struct Contact: Decodable, CustomStringConvertible {
var id: String
@NestedKey
var firstname: String
@NestedKey
var lastname: String
@NestedKey
var address: String
enum CodingKeys: String, NestableCodingKey {
import Foundation
extension String.StringInterpolation {
fileprivate static let myMeasurementFormatter: MeasurementFormatter = {
let this = MeasurementFormatter()
this.unitOptions = [.naturalScale]
this.numberFormatter.maximumFractionDigits = 1
return this
}()
mutating func appendInterpolation<UnitType>(_ measurement: Measurement<UnitType>) where UnitType: Unit {
#!/usr/bin/env xcrun -sdk macosx swift
// Displays UI in an NSWindow which can interact with the commandline
// Usage: `echo "Bar" | ./swift-ui-commandline-tool.swift`
import Foundation
import SwiftUI
extension CommandLine {
static let input: String = { AnyIterator { readLine() }.joined() }()