Skip to content

Instantly share code, notes, and snippets.

Avatar

Rob Ryan robertmryan

View GitHub Profile
View Mean.swift
extension Sequence where Element: FloatingPoint {
func mean() -> Element {
var count = 0
var sum: Element = 0
for element in self {
sum += element
count += 1
}
return sum / Element(count)
View TimerTest.swift
import SwiftUI
import Combine
struct TimerTest: View {
@State var date = Date()
@State var showSubView = false
@State var timer: AnyCancellable? = nil
var body: some View {
ZStack {
View ConcurrentImageLoad.swift
private func loadImagesConCurrent() {
let concurrentQueue = DispatchQueue(label: "myConcurrentQueue", qos: .background, attributes: .concurrent)
activity.startAnimating()
let group = DispatchGroup()
for (i, item) in myArray.enumerated() {
group.enter()
concurrentQueue.async { [weak self] in
let fileURL = try! FileManager.default
.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
.appendingPathComponent(item as! String)
View GenerateRandomColors.swift
func generateRandomColors() -> [UIColor] {
var randomColors: [UIColor] = Array(repeating: .blue, count: .random(in: 0...10))
randomColors += Array(repeating: .red, count: .random(in: 0...15))
randomColors += Array(repeating: .yellow, count: .random(in: 0...25))
randomColors += Array(repeating: .orange, count: 100 - randomColors.count)
return randomColors.shuffled()
}
View String+Regex.swift
extension String {
func matches(of regex: String, options: NSRegularExpression.Options = []) throws -> [String] {
try NSRegularExpression(pattern: regex, options: options)
.matches(in: self, range: NSRange(startIndex..., in: self))
.compactMap { Range($0.range, in: self) }
.map { String(self[$0]) }
}
}
View ViewController.swift
class DriverAnnotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D // if this can change, I'd suggest making it `dynamic var`; if it can't change, I'd make it constant with `let`
var uid: String
init(uid: String, coordinate: CLLocationCoordinate2D) {
self.uid = uid
self.coordinate = coordinate
}
}
View CGPoint+Centroid.swift
//
// CGPoint+Centroid.swift
//
// Created by Robert Ryan on 5/19/20.
// Copyright © 2020 Robert Ryan. All rights reserved.
//
// See https://stackoverflow.com/a/61884774/1271826
import Foundation
View CreateMultipartRequest.swift
enum ConversionError: Error {
case unableToConvertToPng
}
/// Create body of the `multipart/form-data` request
///
/// - parameter parameters: The optional dictionary containing keys and values to be passed to web service
/// - parameter filePathKey: The optional field name to be used when uploading files. If you supply paths, you must supply filePathKey, too.
/// - parameter paths: The optional array of file paths of the files to be uploaded
/// - parameter boundary: The `multipart/form-data` boundary
View SubtleWaveFuncVariation.swift
func f(_ x: CGFloat) -> CGFloat {
let elapsed = CGFloat(elapsed)
let amplitude = maxAmplitude * sin(elapsed)
let amplitude2 = maxAmplitude * sin(elapsed / 1.5)
let variation = sin((elapsed + amplitudeOffset) / amplitudeChangeSpeedFactor) * maxTidalVariation
let value = sin((elapsed / saveSpeedFactor + x) * 4 * .pi) * amplitude / 2
let subwaveValue = sin((elapsed / saveSpeedFactor + x) * 7 / 3 * .pi) * amplitude2 / 5
return value * bounds.height + subwaveValue * bounds.height + (defaultTidalHeight + variation) * bounds.height
}
View WavyView.swift
// WavyView.swift
//
// Created by Robert Ryan on 3/18/20.
import UIKit
@IBDesignable
class WavyView: UIView {
// MARK: - Properties
You can’t perform that action at this time.