Skip to content

Instantly share code, notes, and snippets.

View djryanash's full-sized avatar
🤓

Ryan Ashton djryanash

🤓
View GitHub Profile
@djryanash
djryanash / ViewController.swift
Created May 15, 2023 05:38
Generic UITableView with Headers
import UIKit
class ViewController: UIViewController {
var hasSearched: Bool = false
let peopleArray = [
//["primaryImage", "personNext", "personJobTitle", "selectedImage", "section"],
["dollarsign.circle", "Ben Shapiro", "Accounts Manager", "dollarsign.circle.fill", "Core Team"],
["hammer.circle", "Robert Bokelman", "Operations Manager", "hammer.circle.fill", "Supplemental Team"],
@djryanash
djryanash / ViewController.swift
Last active May 15, 2023 01:52
Function to print the memory usage of whatever you pass in - struct/class/function/variable/constant, etc
class ViewController: UIViewController {
override func viewDidLoad() {
self.checkMemoryUsage(self.tableView) }
func checkMemoryUsage<R>(_ thingToCheck: R, name: String = #function) {
// You can pass in almost anything - a variable/constant/struct/class/function etc.
let size = MemoryLayout.size(ofValue: thingToCheck)
let stride = MemoryLayout.stride(ofValue: thingToCheck)
let alignment = MemoryLayout.alignment(ofValue: thingToCheck)
@djryanash
djryanash / HomeViewController.swift
Created May 12, 2023 09:51
Various UIKit controls which interact with each other and are created programmatically.
import UIKit
class HomeViewController: UIViewController {
var textField: UITextFieldPadded = {
let textField = UITextFieldPadded()
textField.translatesAutoresizingMaskIntoConstraints = false
textField.placeholder = "Enter some text here"
return textField }()
@djryanash
djryanash / AnyClass.swift
Created May 12, 2023 09:35
Testing a REST API POST Request
class AnyClass {
func restApiPostRequest() {
let url = URL(string: "https://reqres.in/api/users")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let userData = ["name": "John", "position": "Software Engineer", "age": "37"]
do { let jsonData = try JSONSerialization.data(withJSONObject: userData, options: .prettyPrinted)
request.httpBody = jsonData }
@djryanash
djryanash / Solution.swift
Created May 12, 2023 09:24
Roman numerals to Int method
import Foundation
class Solution {
func romanToInt(_ s: String) -> Int {
var stringVar = s
print("stringVar: \(stringVar)")
var num = 0
let validDic = [
@djryanash
djryanash / ContentView.swift
Created May 12, 2023 09:20
Line chart in SwiftUI
import SwiftUI
struct DataPoint {
let value: Double
}
struct LineChartShape: Shape {
let dataPoints: [DataPoint]
let pointSize: CGFloat
@djryanash
djryanash / AnyStruct.swift
Created May 12, 2023 03:57
A neat and readable way of evaluating multiple Bool conditions
import Foundation
struct AnyStruct {
func isUserEligible(age: Int, city: String, income: Int, educationLevel: String) {
let isAgeElig: Bool = age >= 18
let isCityElig: Bool = city == "New York" || city == "Los Angeles" || city == "Miami"
let isIncomeElig: Bool = income >= 40_000 && income <= 100_000
let isEduLevelElig: Bool = educationLevel == "Bachelors" || educationLevel == "Masters"
@djryanash
djryanash / ViewController.swift
Created May 11, 2023 09:30
UILabel updates automatically when UITextField is updated in UIKit (surprisingly hard to do).
import UIKit
class HomeViewController: UIViewController, UITextFieldDelegate {
let style = UITraitCollection().userInterfaceStyle
var textField: UITextField = {
let textField = UITextField()
textField.translatesAutoresizingMaskIntoConstraints = false
textField.backgroundColor = .systemBackground
@djryanash
djryanash / UILabelPadded.swift
Created May 11, 2023 06:34
Swift UILabel subclass to add padding.
class UILabelPadded: UILabel {
var textEdgeInsets = UIEdgeInsets(
top: 5,
left: 10,
bottom: 5,
right: 10)
open override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
let insetRect = bounds.inset(by: textEdgeInsets)
@djryanash
djryanash / UITextFieldPadded.swift
Created May 11, 2023 06:04
Swift UITextField subclass to add padding.
class UITextFieldPadded: UITextField {
var textPadding = UIEdgeInsets(
top: 5,
left: 10,
bottom: 5,
right: 10)
override func textRect(forBounds bounds: CGRect) -> CGRect {
let rect = super.textRect(forBounds: bounds)