Skip to content

Instantly share code, notes, and snippets.

View morsedan's full-sized avatar
💭
Open to New Opportunities

Dan Morse morsedan

💭
Open to New Opportunities
View GitHub Profile
@morsedan
morsedan / View+OpenUrl.swift
Created January 29, 2025 05:29 — forked from JamesSedlacek/View+OpenUrl.swift
This file provides a safe way to open URLs in SwiftUI applications.
//
// View+OpenUrl.swift
//
// Created by James Sedlacek on 11/26/24.
//
/// This file provides a safe way to open URLs in SwiftUI applications.
/// It adds a view modifier that handles URL opening with user confirmation
/// and multiple opening options (browser, in-app, or copy to clipboard).
///
enum Status: Decodable {
case completed, inProgress
case other(String)
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let value = try container.decode(String.self)
switch value {
case "completed": self = .completed
@morsedan
morsedan / Networking.swift
Last active June 5, 2020 21:14
Simple Networking Solution in Swift
/*
These three methods can be used for making network calls less repetitive. Apple's example code for a basic
network call is typically around 45 lines. Using these helper methods along with methods to build up each
URL, most network calls can be completed in about 15 lines.
*/
private func perform(_ request: URLRequest, completion: @escaping (Result<Data, Error>) -> Void) {
let dataTask = URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error {
@morsedan
morsedan / ShiftableViewController.swift
Created January 10, 2020 20:21 — forked from SpencerCurtis/ShiftableViewController.swift
A UIViewController subclass that manages shifting the view controller up when editing a UITextField or UITextView that would normally be obscured by they keyboard popping up so that the text input is visible.
//
// ShiftableViewController.swift
//
// Copyright © 2017 Spencer Curtis. All rights reserved.
//
/*
All you need to do is set your subclass of ShiftableViewController as the delegate for all
UITextFields and UITextViews that you want to be shifted up so the keyboard doesn't obscure it.
func flipMatrix(_ A: [[Int]]) -> [[Int]] {
var flipped: [[Int]] = []
for i in 0..<A[0].count {
flipped.append(contentsOf: [[]])
for j in 0..<A.count {
flipped[i].append(A[j][i])
}
}
return flipped
import Foundation
func findDisappearedNumbers(_ nums: [Int]) -> [Int] {
var missingNumbers: [Int] = []
let sortedNumbers = Array(Set(nums).sorted())
for number in 1..<sortedNumbers[0] {
missingNumbers.append(number)
}
func sum(_ nums: [Int], _ target: Int) -> [Int] {
var first = 0
var second = 1
for i in 0...nums.count - 1 {
for j in i + 1...nums.count - 1 {
if nums[i] + nums[j] == target {
first = i
second = j
return [i, j]
}
func fizzBuzz(_ n: Int) -> [String] {
if n == 0 {
return []
}
var start: Int {
if n >= 1 { return 1 }
else if n < 0 { return n }
else { return 0 }
}
let arr1 = [2,3,1,3,2,4,6,7,9,2,19]
let arr2 = [2,1,4,3,9,6]
func order(_ array1: [Int], by array2: [Int]) -> [Int] {
var array1 = array1
var endingArray: [Int] = []
var workingArray: [Int] = []
for number in array2 {
workingArray = array1.filter { $0 == number }
func heightChecker(_ heights: [Int]) -> Int {
let sortedHeights = heights.sorted()
var moveCount = 0
for i in 0..<heights.count {
if sortedHeights[i] != heights[i] {
moveCount += 1
}
}