Skip to content

Instantly share code, notes, and snippets.

williamhqs / Test.playground
Last active May 13, 2020
ProperyWrapper decode
View Test.playground
import UIKit
let jsonString =
"name": "Tim",
"age": "28"
View hometask
/* 3.1 */
// 1. Write Pseudo code find a given element in an array
func linearSearch<T: Equatable>(target: T, in array: [T]) {
for (index, element) in array.enumerated() {
if element == target {
print("Found \(element) at \(index)")
} else if (index == array.count) {
print("The target \(target) not found")
View Date extension
extension Date {
func yearsCount(from date: Date) -> Int {
return Calendar.current.dateComponents([.year], from: date, to: self).year ?? 0
func monthsCount(from date: Date) -> Int {
return Calendar.current.dateComponents([.month], from: date, to: self).month ?? 0
williamhqs /
Created Oct 23, 2018 — forked from Tokuriku/
Script to put in an Aggregate Target of a Framework in Xcode 6 to create a Universal Framework
# make sure the output directory exists
# Step 1. Build Device and Simulator versions
xcodebuild -target "${PROJECT_NAME}" ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphoneos BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean build
xcodebuild -target "${PROJECT_NAME}" -configuration ${CONFIGURATION} -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean build
View universal_framework_script
set -e
williamhqs / GradientLine.swift
Last active Dec 12, 2019
CoreGraphics draw a line with gradient and lineCap
View GradientLine.swift
class GradientLineView: UIView {
override func draw(_ rect: CGRect) {
if let context = UIGraphicsGetCurrentContext() {
let startPoint1 = CGPoint(x: 20, y: rect.height/2)
let endPoint1 = CGPoint(x: rect.width-120, y: rect.height/2)
context.move(to: startPoint1)
context.addLine(to: endPoint1)
View test.swif
View rules.markdown

View Layer Architecture

In a viewController usually there are many methods. In order make this part organized well. Here are the rules:

// MARK: - Life cycle
View Swift_string_contains_chinese.swift
enum ChineseRange {
case notFound, contain, all
extension String {
var findChineseCharacters: ChineseRange {
guard let a = self.range(of: "\\p{Han}*\\p{Han}", options: .regularExpression) else {
return .notFound
var result: ChineseRange
williamhqs /
Created Nov 15, 2016 — forked from tobiasschuerg/
Android Arrayadapter with text filtering for the use with a TextWatcher.
* Arrayadapter (for Android) with text filtering for the use with a TextWatcher.
* Note: the objects in the List need a valid toString() method.
* @author Tobias Schürg
public class FilteredArrayAdapter extends ArrayAdapter<ImageObject> {