Skip to content

Instantly share code, notes, and snippets.

class CustomAnnotation: MKPointAnnotation, Codable {
var imageName: String?
enum CodingKeys: String, CodingKey {
case latitude, longitude, title, subtitle, imageName
}
override init() {
super.init()
}
class CustomAnnotationView: MKAnnotationView {
override var annotation: MKAnnotation? { didSet { update(for: annotation) } }
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
canShowCallout = true
update(for: annotation)
}
override func prepareForReuse() {
/// Suspendable Task
///
/// This provides an interface by which one can write code that can periodically call
/// `waitIfSuspended`, to achieve a block of code that can be suspended.
///
/// Usage:
///
/// ```
/// let item = SuspendableTask { task in
/// for i in 0 ..< 1_000_000 {
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)
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 {
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)
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()
}
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]) }
}
}
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
}
}
//
// 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