Skip to content

Instantly share code, notes, and snippets.

@foxicode
foxicode / Info.plist
Created August 18, 2023 22:08
Info.plist file with scene configuration
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
<false/>
<key>UISceneConfigurations</key>
<dict>
@foxicode
foxicode / AppDelegate.swift
Created August 18, 2023 22:00
App Delegate with scenes
import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
// MARK: UISceneSession Lifecycle
@foxicode
foxicode / Swift+trimmed.swift
Created July 29, 2023 13:25
Removing empty spaces in String (Swift)
public extension String {
var trimmed: String {
trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
}
func trim() -> String {
trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
}
}
@foxicode
foxicode / String+withFirstCapitalized.swift
Created July 29, 2023 13:24
Capitalizing first letter in String (Swift)
public extension String {
var withFirstCapitalized: String {
if isEmpty {
return self
}
let first = substring(start: 0, length: 1)
let other = count > 1 ? self.substring(start: 1, length: count - 1) : ""
return "\(first.uppercased())\(other)"
}
@foxicode
foxicode / String+substring.swift
Created July 29, 2023 13:22
Easy substrings in Swift
public extension String {
func substring(start: Int, length: Int) -> String {
if start >= count {
return ""
}
let startIndex = index(self.startIndex, offsetBy: start)
let endIndex: Index
if start + length > count {
endIndex = index(self.startIndex, offsetBy: count)
@foxicode
foxicode / String+asArray.swift
Created July 29, 2023 13:21
String to character array conversion in Swift
public extension String {
var asArray: [Character] {
map { $0 }
}
func toArray() -> [Character] {
map { $0 }
}
}
@foxicode
foxicode / String+localized.swift
Created July 29, 2023 13:20
Syntactic sugar for Swift localization
public extension String {
var localized: String {
NSLocalizedString(self, comment: "")
}
}
@foxicode
foxicode / String+html.swift
Created July 29, 2023 13:13
HTML to Attributed String conversion in Swift
public extension String {
var html: NSAttributedString {
guard let data = data(using: .utf8) else { return NSAttributedString() }
do {
return try NSAttributedString(
data: data,
options: [
NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html,
NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue
@foxicode
foxicode / String+asUIColor.swift
Last active July 29, 2023 13:14
String to UIColor conversion in Swift
public extension String {
var asUIColor: UIColor? {
var cString = self.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
if cString.hasPrefix("#") {
cString.remove(at: cString.startIndex)
}
let allowedCharacters = CharacterSet(charactersIn: "0123456789ABCDEF")
if cString.rangeOfCharacter(from: allowedCharacters.inverted) != nil {
@foxicode
foxicode / Encodable+copyCodable.swift
Created July 29, 2023 13:05
Encodable copyCodable Swift extension
public extension Encodable where Self: Decodable {
func copyCodable() throws -> Self {
let data = try JSONEncoder().encode(self)
return try JSONDecoder().decode(Self.self, from: data)
}
}