Skip to content

Instantly share code, notes, and snippets.

@foxicode
foxicode / FrameworkViewController.swift
Created May 25, 2020 18:55
Dynamic version of FrameworkViewController
import UIKit
import Darwin
class FrameworkViewController: UIViewController {
@IBOutlet weak var vPluginContainer: UIView!
var timer: Timer?
typealias startFuncPrototype = @convention(c) (Double, Double) -> Void
typealias tickFuncPrototype = @convention(c) (TimeInterval) -> Void
@foxicode
foxicode / String+trim.swift
Last active August 10, 2020 12:12
Swift trimming extension
import Foundation
extension String {
var trimmed: String {
self.trimmingCharacters(in: .whitespacesAndNewlines)
}
mutating func trim() {
self = self.trimmed
}
@foxicode
foxicode / IntDouble.swift
Last active August 10, 2020 12:12
Conversions between Int and Double
import Foundation
extension Int {
func toDouble() -> Double {
Double(self)
}
}
extension Double {
func toInt() -> Int {
@foxicode
foxicode / StringDate.swift
Last active August 10, 2020 12:11
Conversions between String and Double
import Foundation
extension String {
func toDate(format: String) -> Date? {
let df = DateFormatter()
df.dateFormat = format
return df.date(from: self)
}
}
@foxicode
foxicode / Int+centsToDollars.swift
Last active August 10, 2020 12:11
Converting cents to dollars
import Foundation
extension Int {
func centsToDollars() -> Double {
Double(self) / 100
}
}
@foxicode
foxicode / String+asCoordinates.swift
Last active August 10, 2020 12:10
Converting String to Coordinates
import Foundation
import CoreLocation
extension String {
var asCoordinates: CLLocationCoordinate2D? {
let components = self.components(separatedBy: ",")
if components.count != 2 { return nil }
let strLat = components[0].trimmed
let strLng = components[1].trimmed
if let dLat = Double(strLat),
@foxicode
foxicode / String+asURL.swift
Last active August 10, 2020 12:10
Converting String to URL
import Foundation
extension String {
var asURL: URL? {
URL(string: self)
}
}
@foxicode
foxicode / UIDevice+vibrate.swift
Created August 10, 2020 12:02
Making vibration effect
import UIKit
import AudioToolbox
extension UIDevice {
static func vibrate() {
AudioServicesPlaySystemSound(1519)
}
}
@foxicode
foxicode / String+size.swift
Last active August 21, 2020 05:54
Getting String width and height for provided UIFont
import UIKit
extension String {
func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat {
let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [.font: font], context: nil)
return ceil(boundingBox.height)
}
@foxicode
foxicode / String+containsOnlyDigits.swift
Last active August 10, 2020 12:09
Check if String contains only digits
import Foundation
extension String {
var containsOnlyDigits: Bool {
let notDigits = NSCharacterSet.decimalDigits.inverted
return rangeOfCharacter(from: notDigits, options: String.CompareOptions.literal, range: nil) == nil
}
}