Skip to content

Instantly share code, notes, and snippets.

@DavidPiper94
DavidPiper94 / ViewControllerResult.json
Last active April 9, 2020 05:28
Example code for article about SourceKitten - Result of analyzing ViewController
{
"key.substructure" : [
{
"key.namelength" : 14,
"key.elements" : [
{
"key.offset" : 36,
"key.kind" : "source.lang.swift.structure.elem.typeref",
"key.length" : 16
}
@DavidPiper94
DavidPiper94 / ViewController.swift
Last active April 9, 2020 05:28
Example code for article about SourceKitten - ViewController
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
@DavidPiper94
DavidPiper94 / EverythingVisitor.swift
Created April 5, 2020 14:53
Class to visit every node available in SwiftSyntax
//
// EverythingVisiter.swift
// ProjectAnalysis
//
// Created by David Piper in 2019
//
import Foundation
import SwiftSyntax
@DavidPiper94
DavidPiper94 / Reset+Export.swift
Last active March 28, 2020 10:42
Example code for article about drawing - Reset + Export
func resetDrawing() {
// 1
lineArray = []
setNeedsDisplay()
}
func exportDrawing() -> UIImage? {
// 2
@DavidPiper94
DavidPiper94 / AddPoints.swift
Created March 28, 2020 09:51
Example code for article about drawing - Adding points
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// 1
guard let touch = touches.first else { return }
let firstPoint = touch.location(in: self)
// 2
lineArray.append([CGPoint]())
lineArray[lineArray.count - 1].append(firstPoint)
}
@DavidPiper94
DavidPiper94 / Drawing.swift
Last active March 28, 2020 10:42
Example code for article about drawing - Drawing
// 1
override func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else { return }
draw(inContext: context)
}
func draw(inContext context: CGContext) {
// 2
context.setLineWidth(5)
@DavidPiper94
DavidPiper94 / View.swift
Last active March 28, 2020 10:42
Example code for article about drawing - DrawingView
class DrawView: UIView {
// 1
private var lineArray: [[CGPoint]] = [[CGPoint]]()
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// 2
guard let touch = touches.first else { return }
let firstPoint = touch.location(in: self)
@DavidPiper94
DavidPiper94 / ChartsInSwiftUI.swift
Last active March 24, 2020 07:24
Example code for article about radar chart - Using Charts with SwiftUI
// 1
struct RadarView: UIViewRepresentable {
// 2
typealias UIViewType = RadarChartView
// 3
func makeUIView(context: UIViewRepresentableContext<RadarView>) -> RadarChartView {
let radarChart = RadarChartView()
// Setup radar Chart
@DavidPiper94
DavidPiper94 / CustomDimension.swift
Last active March 12, 2020 10:16
Example code for article about Units and Measurements - Creating a custom Dimension
// 1
class CustomRadioactivityUnit: Dimension {
// 2
static let becquerel = CustomRadioactivityUnit(symbol: "Bq", converter: UnitConverterLinear(coefficient: 1.0))
static let curie = CustomRadioactivityUnit(symbol: "Ci", converter: UnitConverterLinear(coefficient: 3.7e10))
// 3
static let baseUnit = becquerel
}
@DavidPiper94
DavidPiper94 / ExtendingDimension.swift
Last active March 12, 2020 10:16
Example code for article about Units and Measurements - Extending an existing Dimension
// 1
extension UnitIlluminance {
// 2
static let footCandle = UnitIlluminance(symbol: "fc", converter: UnitConverterLinear(coefficient: 10.76))
}
// 3
let footCandle = Measurement(value: 1, unit: UnitIlluminance.footCandle)
let lumen = footCandle.converted(to: UnitIlluminance.lux)