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
// | |
// LocationsManager.swift | |
// MyApp | |
// | |
// Created by Robert Ryan on 12/24/22. | |
// | |
import Foundation | |
import CoreLocation |
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 { | |
@IBOutlet var label: UILabel! | |
private weak var timer: Timer? | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
startTimer() | |
} |
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 benchmark() { | |
let string = String(repeating: "This is it. ", count: 10000) | |
let i1 = 19000 | |
let i2 = 19020 | |
// time computation | |
func tc(computation: (Int, Int) -> Void) { | |
let startTime = DispatchTime.now() | |
for i in 0..<100 { | |
computation(i1 + i, i2 + i) |
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 search() async throws -> BusinessSearchResult { | |
var components = URLComponents(string: "https://api.yelp.com/v3/businesses/search") | |
components?.queryItems = [ | |
URLQueryItem(name: "term", value: "theater"), | |
URLQueryItem(name: "location", value: "NYC") | |
] | |
guard let url = components?.url else { throw URLError(.badURL) } |
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 NetworkManager { | |
let session: URLSession = { | |
let session = URLSession(configuration: .default) | |
return session | |
}() | |
deinit { | |
session.finishTasksAndInvalidate() | |
} | |
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 foo(threads: Int) { | |
print("starting \(threads) threads") | |
let semaphore = DispatchSemaphore(value: 0) | |
let group = DispatchGroup() | |
for i in 0 ..< threads { | |
DispatchQueue.global().async(group: group) { | |
semaphore.wait() | |
print("done waiting \(i)") |
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 |
NewerOlder