Skip to content

Instantly share code, notes, and snippets.

View codelynx's full-sized avatar

Kaz Yoshikawa codelynx

View GitHub Profile
@codelynx
codelynx / ZArchivable.swift
Created October 8, 2016 06:49
Utility code for bulk archiving or bulk unarchiving for `struct`s (swift 3.0)
//
// ZArchivable.swift
// ZKit
//
// The MIT License (MIT)
//
// Copyright (c) 2016 Electricwoods LLC, Kaz Yoshikawa.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@codelynx
codelynx / CoreGraphics+Z.swift
Last active November 28, 2016 04:28
CoreGraphics Geometric Utilities [Swift 3.0]
//
// GeoUtils.swift
// ZKit
//
// The MIT License (MIT)
//
// Copyright (c) 2016 Electricwoods LLC, Kaz Yoshikawa.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@codelynx
codelynx / ZWeakSet.swift
Last active July 22, 2018 19:33
ZWeakSet that collection of hashable weak set of objects [swift 3.0]
//
// ZWeakSet.swift
// ZTableLayout
//
// The MIT License (MIT)
//
// Copyright (c) 2016 Electricwoods LLC, Kaz Yoshikawa.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@codelynx
codelynx / Array+Shuffled.swift
Last active October 24, 2016 04:05
Generate shuffled array in swift 3.0
//
// ZArray+Z.swift
// ZKit
//
// The MIT License (MIT)
// Copyright (c) 2016 Electricwoods LLC, Kaz Yoshikawa. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
@codelynx
codelynx / NSNumber+makeNumber.swift
Created October 27, 2016 04:02
Utility piece of code to make NSNumber from optional value
//
// NSNumber+Z.swift
// ZKit
//
// The MIT License (MIT)
//
// Copyright (c) 2016 Electricwoods LLC, Kaz Yoshikawa.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@codelynx
codelynx / ZObservable.swift
Last active November 7, 2016 14:13
Lightweight utility code to implement change propagation mechanism from observable objects to observer objects.
// ZObservable
// ZKit
//
// The MIT License (MIT)
//
// Copyright (c) 2016 Electricwoods LLC, Kaz Yoshikawa.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
//
// ZWeakSet.swift
// ZKit
//
// The MIT License (MIT)
//
// Copyright (c) 2016 Electricwoods LLC, Kaz Yoshikawa.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@codelynx
codelynx / mac_exec_cmd_line_snippet.swift
Created November 15, 2016 12:53
Code snippet to execute command line with arguments [Mac] [Swift]
let process = Process()
process.launchPath = "/usr/sbin/arp"
process.arguments = ["-a"]
let pipe = Pipe()
process.standardOutput = pipe
process.launch()
let output = pipe.fileHandleForReading.readDataToEndOfFile()
let outputString = String(bytes: output, encoding: .utf8)!
print(outputString)
@codelynx
codelynx / gcd_semaphore.swift
Last active November 15, 2016 16:27
[swift 3] code snippet using GCD's semaphore to protect accessing critical resources from multiple threads where wait and signal may not be the same thread.
class MyObject {
let semaphore = DispatchSemaphore(value: 1)
let session = URLSession(configuration: URLSessionConfiguration.default)
func update1() {
// don't call from mail thread
print("update1 - begin")
semaphore.wait()
let url = URL(string: "https://www.apple.com/")!
let task = self.session.dataTask(with: url) { data, response, error in
@codelynx
codelynx / gcd_semaphore.swift
Created November 15, 2016 16:24
[swift3] using GCD's semaphore to protect critical resources accessing from multiple threads.
class MyObject {
let semaphore = DispatchSemaphore(value: 1)
func update1() {
semaphore.wait()
defer { semaphore.signal() }
// update some critical resources
}