Skip to content

Instantly share code, notes, and snippets.

@nhatlee
nhatlee / iPod.swift
Created July 28, 2020 03:00 — forked from jordansinger/iPod.swift
Swift Playgrounds iPod Classic
import SwiftUI
import PlaygroundSupport
struct iPod: View {
var body: some View {
VStack(spacing: 40) {
Screen()
ClickWheel()
Spacer()
}
// Original article here: https://www.fivestars.blog/code/swiftui-hierarchy-list.html
import SwiftUI
struct FileItem: Identifiable {
let name: String
var children: [FileItem]?
var id: String { name }
@nhatlee
nhatlee / libdispatch-efficiency-tips.md
Created February 18, 2020 13:42 — forked from tclementdev/libdispatch-efficiency-tips.md
Making efficient use of the libdispatch (GCD)

libdispatch efficiency tips

I suspect most developers are using the libdispatch inefficiently due to the way it was presented to us at the time it was introduced and for many years after that, and due to the confusing documentation and API. I realized this after reading the 'concurrency' discussion on the swift-evolution mailing-list, in particular the messages from Pierre Habouzit (who is the libdispatch maintainer at Apple) are quite enlightening (and you can also find many tweets from him on the subject).

My take-aways are:

@nhatlee
nhatlee / Demo.swift
Created February 12, 2020 09:58 — forked from AliSoftware/Demo.swift
NestableCodingKey: Nice way to define nested coding keys for properties
struct Contact: Decodable, CustomStringConvertible {
var id: String
@NestedKey
var firstname: String
@NestedKey
var lastname: String
@NestedKey
var address: String
enum CodingKeys: String, NestableCodingKey {
//------------------------------------------------------------------------
// The SwiftUI Lab: Advanced SwiftUI Animations
// https://swiftui-lab.com/swiftui-animations-part1 (Animating Paths)
// https://swiftui-lab.com/swiftui-animations-part2 (GeometryEffect)
// https://swiftui-lab.com/swiftui-animations-part3 (AnimatableModifier)
//------------------------------------------------------------------------
import SwiftUI
struct ContentView: View {
@nhatlee
nhatlee / StreamReader.swift
Created January 16, 2019 09:01 — forked from sooop/StreamReader.swift
Read a large text file line by line - Swift 3
import Foundation
class StreamReader {
let encoding: String.Encoding
let chunkSize: Int
let fileHandle: FileHandle
var buffer: Data
let delimPattern : Data
var isAtEOF: Bool = false
@nhatlee
nhatlee / gist:3459ee5b971b9a6fe2576b594ad113b1
Created November 29, 2018 04:02 — forked from Nyx0uf/gist:217d97f81f4889f4445a
UIImage scale using vImage
-(UIImage*)mmg_imageScaledToFitSize:(CGSize)fitSize
{
// Create a vImage_Buffer from the CGImage
CGImageRef sourceRef = self.CGImage;
vImage_Buffer srcBuffer;
vImage_CGImageFormat format = {
.bitsPerComponent = 8,
.bitsPerPixel = 32,
.colorSpace = NULL,
.bitmapInfo = (CGBitmapInfo)kCGImageAlphaFirst,
protocol DogBreed {
typealias Level = Int
var breedName: String { get set }
var size: Level { get set }
var health: Level { get set }
var adaptability: Level { get set }
var intelligence: Level { get set }
var dogType: DogType { get set }
import UIKit
import RxSwift
import RxCocoa
let requiredUserNameLength = 5
let requiredPasswordLength = 5
let limitUserNameLength = 20
class ValidationViewController: UIViewController {
@nhatlee
nhatlee / array_structure.swift
Created August 1, 2018 07:04 — forked from austinzheng/array_structure.swift
A horrendous example of custom pattern matching in Swift
// Playground - noun: a place where people can play
import Cocoa
enum Wildcard : NilLiteralConvertible {
case Single
case FromBeginning
case ToEnd
case Range(Int)
case Literal(Int)