Skip to content

Instantly share code, notes, and snippets.

@foxicode
foxicode / String+isAlphanumeric.swift
Last active August 10, 2020 12:08
Check if String has only alphanumeric characters
import Foundation
extension String {
var isAlphanumeric: Bool {
!isEmpty && range(of: "[^a-zA-Z0-9]", options: .regularExpression) == nil
}
}
@foxicode
foxicode / UIImage+squared.swift
Created August 10, 2020 12:06
Making squared UIImage
import UIKit
extension UIImage {
var squared: UIImage? {
let originalWidth = size.width
let originalHeight = size.height
var x: CGFloat = 0.0
var y: CGFloat = 0.0
var edge: CGFloat = 0.0
@foxicode
foxicode / UIImage+resized.swift
Created August 10, 2020 12:07
Resizing UIImage keeping aspect ratio
import UIKit
extension UIImage {
func resized(maxSize: CGFloat) -> UIImage? {
let scale: CGFloat
if size.width > size.height {
scale = maxSize / size.width
}
else {
scale = maxSize / size.height
@foxicode
foxicode / Int+toString.swift
Created August 10, 2020 12:13
Converting Int to String
import Foundation
extension Int {
func toString() -> String {
"\(self)"
}
}
@foxicode
foxicode / Double+toString.swift
Created August 10, 2020 12:14
Converting Double to String
import Foundation
extension Double {
func toString() -> String {
String(format: "%.02f", self)
}
}
@foxicode
foxicode / Double+toPrice.swift
Created August 10, 2020 12:14
Formatting Double as Price String
import Foundation
extension Double {
func toPrice(currency: String) -> String {
let nf = NumberFormatter()
nf.decimalSeparator = ","
nf.groupingSeparator = "."
nf.groupingSize = 3
nf.usesGroupingSeparator = true
nf.minimumFractionDigits = 2
@foxicode
foxicode / String+asDict.swift
Created August 10, 2020 12:16
Converting JSON String to Swift Dictionary
import Foundation
extension String {
var asDict: [String: Any]? {
guard let data = self.data(using: .utf8) else { return nil }
return try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any]
}
}
@foxicode
foxicode / String+asArray.swift
Created August 10, 2020 12:17
Converting JSON String to Swift Array
import Foundation
extension String {
var asArray: [Any]? {
guard let data = self.data(using: .utf8) else { return nil }
return try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [Any]
}
}
@foxicode
foxicode / String+asAttributedString.swift
Created August 10, 2020 12:18
Parsing HTML and saving into NSAttributedString
import Foundation
extension String {
var asAttributedString: NSAttributedString? {
guard let data = self.data(using: .utf8) else { return nil }
return try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
}
}
@foxicode
foxicode / Bundle+appVersion.swift
Created August 10, 2020 12:19
Getting app version from app bundle
import Foundation
extension Bundle {
var appVersion: String? {
self.infoDictionary?["CFBundleShortVersionString"] as? String
}
static var mainAppVersion: String? {
Bundle.main.appVersion
}