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 }
import Foundation
import XCTest
func checkPhoneWithPatterns(_ phone: String) -> Bool{
let patterns = [
"^(840|84|0|)8[0-9]{8}$",
"(840|84|0|)9\\d{8}",
"^(840|84|0|)5(2|6|8|9)\\d{7}$",
"(840|84|0|)7(0|6|7|8|9)\\d{7}",
"(840|84|0|)3[2-9]\\d{7}"
@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 / PMThreadSafeMutableArray.m
Created January 19, 2018 06:20
Thread safe for NSMutableArray
//
// PMThreadSafeMutableArray.m
//
// Created by nhatlee on 1/19/18.
// Copyright © 2018 nhatlee. All rights reserved.
//
#import "PMThreadSafeMutableArray.h"
#import <libkern/OSAtomic.h>
@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 / ScreenshotDetector
Last active February 26, 2019 04:07
ScreenshotDetector
import UIKit
import Photos
/// A class that detects when the user has taken a screenshot and provides it via a delegate callback.
@available(iOS 9.0, *)
open class ScreenshotDetector: NSObject {
/// An error encountered when detecting and retreiving a screenshot.
public enum Error: Swift.Error {
@nhatlee
nhatlee / LinkedList
Last active February 13, 2019 10:59
Linkedlist Recursive Reverse
class Node {
var data: Int
var next: Node?
// init(_ data: Int) {
// self.data = data
// }
init?(_ values: Array<Int>) {
var valuesCopy = values
if valuesCopy.count == 0 {
@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