Skip to content

Instantly share code, notes, and snippets.

View wotjd's full-sized avatar

wotjd wotjd

  • Seoul, South Korea
View GitHub Profile
public struct ArraySlice<Element> {
...
@inlinable public func map<T>(_ transform: (Element) throws -> T) rethrows -> [T]
...
}
// https://ko.wikipedia.org/wiki/함자_(수학)
struct F<X> {
let x : X?
init(_ x: X?) { self.x = x }
func map<Y>(_ f : @escaping (X) -> Y?) -> F<Y> {
guard let x = self.x else {
return F<Y>(nil)
import Foundation
DispatchQueue.global().sync {
print("hi")
}
print("good")
let serial = DispatchQueue(label: "Serial")
@wotjd
wotjd / GetParameterSet.swift
Last active August 27, 2019 06:46
get h264/hevc parameter set from cmsamplebuffer
import AVFoundation
// Get H264/HEVC Parameter Set
private func getParameterSet(_ sampleBuffer: CMSampleBuffer) -> Data {
var parameterSet = Data()
let codecStartCode = [UInt8](arrayLiteral: 0x00, 0x00, 0x00, 0x01)
parameterSet.append(contentsOf: codecStartCode)
let description = CMSampleBufferGetFormatDescription(sampleBuffer)!
var numParams = 0
@wotjd
wotjd / ReduceUsage.swift
Created August 28, 2019 09:39
combine byte array using reduce
import Foundation
extension ExpressibleByIntegerLiteral {
var bytes: [UInt8] { return withUnsafeBytes(of: self) { Array($0) } }
}
var id = UInt8(exactly: 1)!
var count = UInt8(exactly: 2)!
var timeStart = UInt64(exactly: 0x1234567898765432)!
var timeEnd = UInt64(exactly: 4)!
@wotjd
wotjd / Receivable.swift
Created September 6, 2019 15:09
easily convert json data to model object
import Foundation
protocol Receivable {
init?(from: Data)
}
extension Receivable where Self: Decodable {
init?(from jsonData: Data) {
guard let decoded = try? JSONDecoder().decode(Self.self, from: jsonData) else { return nil }
self = decoded
import Moya
import Foundation
guard let case MoyaError.underlying(nsError as NSError, _) = error else { return }
let unknownHostError = [
NSURLErrorUnknown,
NSURLErrorCannotFindHost,
NSURLErrorDomain,
NSURLErrorCancelled,
@wotjd
wotjd / include_touch_of_other_views.swift
Created November 21, 2019 03:30
A Button Gets Touch Event of Multiple Views (though not subview!)
import UIKit
class CustomButton {
var containingTouchViews: [UIView] = []
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
self.containingTouchViews
.map { view in
view.frame.contains(self.convert(point, to: view))
// below codes are optional
@wotjd
wotjd / roundable_button.swift
Created November 26, 2019 06:52
roundable button
import UIKit
class CustomRoundableButton: UIButton {
@IBInspectable var rounded: Bool = false {
didSet { self.updateCornerRadius() }
}
var cornerRadius: CGFloat?
override func layoutSubviews() {
super.layoutSubviews()
@wotjd
wotjd / convert_strings.swift
Created November 27, 2019 07:21
convert raw id, string file to strings file
/*
id.txt example:
stringid1
stringid2
...
eng.txt example:
hello