Skip to content

Instantly share code, notes, and snippets.

View godrm's full-sized avatar

Jung Kim godrm

  • Codesquad
  • Seoul, KOREA
  • X @godrm
View GitHub Profile
@godrm
godrm / swift_api_guideline.md
Last active March 27, 2024 05:15
스위프트 API 가이드라인

1. 스타일/문법 리뷰

1-1 스위프트 API 디자인 가이드라인

https://swift.org/documentation/api-design-guidelines/

  • 사용할 때 기준으로 명확하게 작성하는 게 가장 중요한 지향점이다. 메소드나 프로퍼티 같은 개발 요소는 한 번만 선언하고 반복적으로 사용한다. API를 만들 때는 사용하기 명확하고 편하게 만들어야 한다. 설계를 검증할 때 선언 부분을 읽는 것만으로는 부족하다. 그 대신 사용하는 상황에서 맥락에 맞고 명확한 지 늘 고려해야 한다.

  • 명확한 표현이 압축한 간결성보다 더 중요하다. 스위프트 코드는 압축해서 간결하게 작성할 수 있지만, 단지 글자수를 줄여서 가장 짧은 코드를 만드는 게 목표는 아니다. 스위프트 코드의 간결성은 자연스럽게 반복적으로 재사용하는 코드(boilerplate)를 줄이는 기능과 강한 타입 시스템의 부수효과로 드러날 뿐이다.

@godrm
godrm / HugeLadderGame.swift
Last active September 8, 2023 10:35
사다라게임 recfatoring
struct HugeLadderGame {
struct LadderPlayer {
var name = ""
}
struct InputView {
static func readHeight() -> Int {
print("사다리 높이를 입력해주세요.")
let height = readLine() ?? ""
return Int(height) ?? 0
}
@godrm
godrm / 1MemoryDump.swift
Last active September 4, 2023 04:35
Swift Variable Address Dump to console
struct Memory {
static func dump<T>(variable: inout T) {
withUnsafePointer(to: &variable) { print($0) }
}
static func dump(with: UnsafeRawPointer) {
let address = Int(bitPattern: with)
print(String(format:"%p", address))
}
@godrm
godrm / EchoServer.swift
Last active November 9, 2022 22:25
EchoServer with Network.Framework
//
// EchoServer.swift
// CS16
//
// Created by JK on 2020/01/14.
// Copyright © 2020 codesquad. All rights reserved.
//
import Foundation
import Network
protocol ViewModelBinding {
associatedtype Key
associatedtype Data
func updateNotify(changed: @escaping (Key, Data)->())
}
class ForecastImageViewModel : ViewModelBinding {
typealias Key = [UIImage]?
typealias Data = (duration: Double, firstImage: UIImage?)
@godrm
godrm / LineFactory.swift
Created April 6, 2022 10:51
HelloPlane 심볼 예제
import Foundation
class LineFactory {
static var lineCount : Int = 0
static func makeLine() -> Line {
return Line()
}
class func randomLine() -> Line {
let line = Line()
class ReadValue {
private var thread : Thread? = nil
init(with handler: @escaping (String) -> ()) {
thread = Thread(block: {
while(true) {
let value = readLine() ?? ""
handler(value)
}
})
thread?.start()
import Foundation
class ReadValue {
var successClosure : ((String)->())? = nil
var value : String = "" {
didSet {
if let closure = successClosure {
closure(value)
}
}
}
@godrm
godrm / AsyncFunc+objc.m
Created November 8, 2020 03:49
AsyncFunc by Objective-C
typedef void (^AVAssetImageGeneratorCompletionHandler)(CMTime, CGImageRef _Nullable, CMTime, AVAssetImageGeneratorResult, NSError * _Nullable);
// ...
- (void)generateCGImagesAsynchronouslyForTimes:(NSArray<NSValue *> *)requestedTimes
completionHandler:(AVAssetImageGeneratorCompletionHandler)handler;
@godrm
godrm / RacyActor+Phase2.swift
Created November 8, 2020 03:46
RacyActor Phase#2
var racyGlobal: [String] = []
@MyGlobalActor
var safeGlobal: [String] = []
class PlainOldClass {
var unprotectedState: String = []
}
actor class RacyActor {