Skip to content

Instantly share code, notes, and snippets.

View mhanlon's full-sized avatar

Matt mhanlon

View GitHub Profile
@mhanlon
mhanlon / AdvancedXcodeWorkshop-PhotoApp-2
Created April 11, 2022 07:42
The third phase of our Advanced Xcode workshop
View AdvancedXcodeWorkshop-PhotoApp-2
import UIKit
struct PhotoInfo: Codable {
var title: String
var description: String
var url: URL
var copyright: String?
enum CodingKeys: String, CodingKey {
@mhanlon
mhanlon / AdvancedXcodeWorkshop-PhotoApp-2.swift
Last active April 11, 2022 07:30
The second phase of our Advanced Xcode workshop
View AdvancedXcodeWorkshop-PhotoApp-2.swift
import UIKit
let urlString = "https://api.nasa.gov/planetary/apod"
var urlComponents = URLComponents(string: urlString)!
urlComponents.queryItems = [
"api_key": "DEMO_KEY",
"date": "2022-04-11"
].map { URLQueryItem(name: $0.key, value: $0.value) }
@mhanlon
mhanlon / AdvancedXcodeWorkshop-PhotoApp-1
Created April 11, 2022 06:41
A snippet of code to get us all on the same page in the Advanced Xcode Workshop
View AdvancedXcodeWorkshop-PhotoApp-1
import UIKit
//let urlString = "https://www.thecodehub.ie/v1/api.json"
let urlString = "https://api.nasa.gov/planetary/apod"//?api_key=DEMO_KEY"
let url = URL(string:urlString)!
var urlComponents = URLComponents(string: urlString)!
urlComponents.queryItems = [
"api_key": "DEMO_KEY",
"date": "2022-04-11"
@mhanlon
mhanlon / naturalLanguage.swift
Last active January 25, 2022 12:06
An example of using the NaturalLanguage framework to detect the dominant language in some text.
View naturalLanguage.swift
import NaturalLanguage
let greeting = "Hello, playground"
let inverno = "In questo libro lo si farà in maniera chiara, precisa e, per quanto possibile, divertente."
let don = "Y luego, en el margen, citar a Horacio, o a quien lo dijo."
let riverrun = "a way a lone a last a loved a long the riverrun, past Eve and Adam's, from swerve of shore to bend of bay, brings us by a commodious vicus of recirculation back to Howth Castle and Environs."
let texts = [inverno, don, riverrun]
func identifyLanguageOf(text: String) {
@mhanlon
mhanlon / StringToUIImage.swift
Created June 23, 2021 10:11
A little extension to String to turn a String into a UIImage?
View StringToUIImage.swift
extension String {
var image: UIImage? {
// Change these values if you want (or need) an image larger than 100x100
let size = CGSize(width: 100, height: 100)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
// Maybe make this respect whether the user has selected light or dark mode...
// but we can also use UIColor.clear...
UIColor.clear.set()
let rect = CGRect(origin: .zero, size: size)
UIRectFill(CGRect(origin: .zero, size: size))
@mhanlon
mhanlon / Advice-main.swift
Created June 28, 2020 07:49
Swan's Quest, Chapter 4
View Advice-main.swift
// This code is for the Advice from the Lizard main.swift file
// It uses the playInstrument function to play an A4 with an electric guitar.
func performance(owner: Assessable) {
// TODO: Play an instrument using the calls the Lizard told you about
playInstrument(.electricGuitar, note: MIDINotes.a4)
// Let the Lizard know when you're done
owner.endPerformance()
}
@mhanlon
mhanlon / main.swift
Created June 26, 2020 13:35
The Advice from the Lizard main.swift code for Swan's Quest, Chapter 3
View main.swift
let notes: [Note] = [Note.quarter(.a4), Note.quarter(.b4), Note.quarter(.c3)]
let toneOutput = ToneOutput()
var noteIndex = 0
Timer.scheduledTimer(withTimeInterval: 0.4, repeats: true) { timer in
guard noteIndex < notes.count else {
toneOutput.stopTones()
timer.invalidate()
return
}
@mhanlon
mhanlon / Music.swift
Last active June 26, 2020 13:24
Swan's Quest: Chapter 3, Music.swift file and Swan's Hall performance main.swift file
View Music.swift
//
// Music.swift
//
// Copyright © 2020 Apple Inc. All rights reserved.
//
public protocol PitchProtocol {
/// The sonic frequency of this Pitch
var frequency: Double { get }