Skip to content

Instantly share code, notes, and snippets.

View nick6969's full-sized avatar
🏠
Working from home

Nick Lin nick6969

🏠
Working from home
View GitHub Profile
@nick6969
nick6969 / Publishers-MoyaRequest.swift
Created November 7, 2021 02:03
Moya with Combine request.
import Combine
import Moya
extension Publishers {
struct MoyaRequest<Target: TargetType, Model: Decodable>: Publisher {
typealias Output = Model
typealias Failure = Error
private let provider: MoyaProvider<Target>
private let target: Target
@nick6969
nick6969 / Publishers-MoyaEmptyRequest.swift
Created November 7, 2021 02:02
Moya with Combine with empty response request.
import Combine
import Moya
extension Publishers {
struct MoyaEmptyRequest<Target: TargetType>: Publisher {
typealias Output = Void
typealias Failure = Error
private let provider: MoyaProvider<Target>
private let target: Target
@nick6969
nick6969 / CodableTool.swift
Created September 30, 2021 16:25
Codable Tool
enum CodableTool {
@propertyWrapper
struct Nullable<T: Codable>: Codable {
var wrappedValue: T?
init(from decoder: Decoder) throws {
let container: SingleValueDecodingContainer = try decoder.singleValueContainer()
wrappedValue = try? container.decode(T.self)
}
@nick6969
nick6969 / NSAttributedStringExtension.swift
Last active September 28, 2021 15:06
NSAttributedString calculator height
extension NSAttributedString {
func calcHeight(for width: CGFloat) -> CGFloat {
guard self.string.count > 0 else { return 0 }
let maxHeight: CGFloat = 10000
let path: CGPath = CGPath(rect: .init(x: 0, y: 0, width: width, height: maxHeight), transform: nil)
let frame: CTFrame = ctFrame(for: path)
// swiftlint:disable force_cast
let lines: [CTLine] = CTFrameGetLines(frame) as! [CTLine]
// swiftlint:enadble force_cast
@nick6969
nick6969 / TransformToResult.swift
Created August 26, 2021 02:38
將 Upstream 的 Output 跟 Failure,轉換成 Result
import Combine
extension Publisher {
func transformToResult() -> Publishers.TransformToResult<Self> {
.init(upstream: self)
}
}
extension Publishers {
struct TransformToResult<Upstream: Publisher>: Publisher {
@nick6969
nick6969 / CodableError.swift
Created August 4, 2021 03:35
Codable DecodingError.TypeMismatch
let json =
"""
{
"2021-06-20-Sun": {
"test": 0
},
"2021-06-21-Mon": {
"test": 0
},
"2021-06-22-Tue": {
import Combine
extension Publisher {
func asError<ErrorType: Error>(type: ErrorType.Type, _ transform: @escaping (Self.Failure) -> ErrorType) -> Publishers.AsError<Self, ErrorType> {
return .init(upstream: self, type: type.self, transform)
}
}
final class TestInstance {
enum TestType {
case abcd
}
private let queue: DispatchQueue = DispatchQueue(label: "TestQueue")
private var showTypes: [TestType] = []
func test01(with type: TestType) {
// [TestInstance.TestType].Index
@nick6969
nick6969 / UDTool.swift
Created January 29, 2021 08:10
UserDefault Using @propertyWrapper
enum UDTool {
fileprivate
enum Key: String {
case sample
}
@UDGeneric<String>(.sample, "") static var sample: String
}
@nick6969
nick6969 / convert.swift
Last active July 27, 2020 02:24
hexString to Decimal
enum ConvertDecimalError: Error {
case InvalidateValue
}
func converHexStringToDecimal(_ hex: String) throws -> Decimal {
if !hex.hasPrefix("0x") {
throw ConvertDecimalError.InvalidateValue
}
let strideCount: Int = 10
let hexValue = hex.suffix(hex.count-2)