This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import os.log | |
private let logger = Logger(subsystem: "MyApp", category: "ViewController") | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var task: Task<Void, Error>? | |
func updateLabelAfterAPICall(initialValue: String) { | |
task?.cancel() // just in case | |
task = Task { [weak self] in | |
try await Task.sleep(nanoseconds: 5_500_000_000) | |
self?.label.text = "New Value after 5.5 seconds passed" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController { | |
lazy var animator = UIDynamicAnimator(referenceView: view) | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let animatedView = UIView(frame: CGRect(x: 120, y: 100, width: 50, height: 50)) | |
animatedView.transform = CGAffineTransform(rotationAngle: .pi / 4) | |
animatedView.backgroundColor = .blue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct HoundResponse<T: Decodable>: Decodable { | |
var message: T? | |
var status: String | |
} | |
let url = URL(string: "https://dog.ceo/api/breed/hound/images")! | |
URLSession.shared.dataTask(with: url) { data, _, error in | |
guard let data = data, error == nil else { | |
print(error ?? URLError(.badServerResponse)) | |
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func fetchBook(id identifier: String) async throws -> GoogleBook { | |
var components = URLComponents(string: "https://www.googleapis.com/books/v1/volumes") | |
components?.queryItems = [URLQueryItem(name: "q", value: "{" + identifier + "}")] | |
guard let url = components?.url else { throw URLError(.badURL) } | |
let (data, _) = try await URLSession.shared.data(from: url) | |
return try JSONDecoder().decode(GoogleBook.self, from: data) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let recognizer = SFSpeechRecognizer() | |
weak var task: SFSpeechRecognitionTask? // note weak reference, as the recognition task keeps strong reference for us while the task is underway | |
deinit { | |
task?.cancel() // if still running by the time we deallocate, stop it | |
} | |
func startRecognition(of fileURL: URL) { | |
task?.cancel() // if prior task running, stop it |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Images { | |
static func minus(pointSize: CGFloat? = nil) -> UIImage? { | |
image(named: "minus", pointSize: pointSize) | |
} | |
static func trash(pointSize: CGFloat? = nil) -> UIImage? { | |
image(named: "trash", pointSize: pointSize) | |
} | |
private static func image(named name: String, pointSize: CGFloat? = nil) -> UIImage? { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func generateMap(with coordinates: [CLLocationCoordinate2D]) { | |
var annotationViews = [MKMarkerAnnotationView]() | |
let startCoord = coordinates.first! | |
let endCoord = coordinates.last! | |
annotationViews.append(createMapMarkerAnnotation(text: "A", coordinates: startCoord, color: .green)) | |
annotationViews.append(createMapMarkerAnnotation(text: "B", coordinates: endCoord, color: .green)) | |
let snapshotter = MKMapSnapshotter(options: options) | |
snapshotter.start { snapshot, error in |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
UIImage *image = [UIImage imageNamed:@"test.png"]; | |
self.imageView.image = [self blackAndWhiteFromImage:image]; | |
} | |
- (UIImage * _Nullable)makeBlackPixelsRedInImage:(UIImage *)image { | |
UInt32 black = [self pixelFromRed:0 green:0 blue:0 alpha:255]; | |
UInt32 red = [self pixelFromRed:255 green:0 blue:0 alpha:255]; |