Skip to content

Instantly share code, notes, and snippets.

View phillipcaudell's full-sized avatar

Phillip Caudell phillipcaudell

View GitHub Profile
@phillipcaudell
phillipcaudell / String+Charset.swift
Created March 4, 2024 09:33
An extension that adds the ability to initialise a String.Encoding from an IANA charset name.
import Foundation
extension String.Encoding {
/// Creates a new instance using the specified IANA charset name.
public init?(charsetName name: String) {
let match = name
.lowercased()
.replacing("-", with: "")
.replacing("_", with: "")
@phillipcaudell
phillipcaudell / FormSheet.swift
Created September 21, 2023 14:17
A presentation style that displays the content centered in the screen smaller than a regular sheet.
import SwiftUI
extension View {
/// Presents a form sheet when a binding to a Boolean value that you provide is true.
/// - Parameters:
/// - isPresented: A binding to a Boolean value that determines whether
/// to present the sheet that you create in the modifier's
/// `content` closure.
/// - interactiveDismiss: A Boolean value that indicates whether to
@phillipcaudell
phillipcaudell / AnyCodable.swift
Created September 19, 2023 09:23
A type-erased codable wrapper
import Foundation
/// A type-erased codable wrapper.
public struct AnyCodable {
public typealias Value = any Codable & Hashable
public let value: Value
/// Creates an instance that type-erases a value.
@phillipcaudell
phillipcaudell / Confirmation.swift
Created September 1, 2023 09:50
Present a confirmation dialogue from a swipeAction, contextMenu or other transient view.
import SwiftUI
/// Describe a potentially destructive action to the user.
///
/// A confirmation describes the action to the user and provides a mechanism for confirming the action in the
/// form of a `Button`. You present a confirmation by calling the environment's confirm action:
///
/// ```swift
/// @Environment(\.confirm) private var confirm
///
@phillipcaudell
phillipcaudell / gist:5909241
Last active December 19, 2015 06:09
Indefinite article for word
- (NSString *)indefiniteArticleForWord:(NSString *)word singularArticle:(NSString *)singularArticle pluralArticle:(NSString *)pluralArticle
{
NSString *firstLetter = [[word lowercaseString] substringToIndex:1];
NSArray *vowels = @[@"a",@"e",@"i",@"o",@"u"];
BOOL isFirstLetterVowel = NO;
for (NSString *vowel in vowels){
if ([vowel isEqualToString:firstLetter]) {
isFirstLetterVowel = YES;
break;