Swift
-
[Swift] @dynamicMemberLookup 써보기Swift 2023. 10. 26. 16:06
GitHub - insub4067/Hello--dynamicMemberLookup Contribute to insub4067/Hello--dynamicMemberLookup development by creating an account on GitHub. github.com @dynamicMemberLookup struct Model { var items: [String: String] subscript(dynamicMember key: String) -> String? { items[key] } } struct Parent { let id: Int let name: String } @dynamicMemberLookup struct Child { let name: String let parent: Par..
-
[Swift] KeyPath, WritableKeyPath, ReferenceWritableKeyPath 차이와 구분해서 사용하기Swift 2023. 10. 25. 17:55
GitHub - insub4067/KeyPath--WritableKeyPath--ReferenceWritableKeyPath Contribute to insub4067/KeyPath--WritableKeyPath--ReferenceWritableKeyPath development by creating an account on GitHub. github.com protocol SelfReturnable { associatedtype ReturnType func with(_ keyPath: WritableKeyPath, _ value: T) -> ReturnType } extension SelfReturnable { func with(_ keyPath: WritableKeyPath, _ value: T) -..
-
[Swift] NSPredicate format placeholdersSwift 2023. 10. 25. 16:59
format placeholders 정리에 대한 GPT 의 답변 In Objective-C, when creating an NSPredicate with a format string, you can use several different placeholders to represent values of different data types. Here are some common placeholders for format strings in NSPredicate: 1. %@: Used for representing object values, such as NSString, NSNumber, NSDate, and other NSObject-derived classes. 2. %K: Used for specif..
-
[Combine] AsyncPublisher 란?Swift 2023. 10. 24. 13:42
struct ContentView: View { @StateObject var viewModel = ContentViewModel() var body: some View { Button("Button") { viewModel.didTap() } } } class ContentViewModel: ObservableObject { @Published var items: [String] = [] private var store = Set() init() { observe() } func didTap() { items = ["1", "2", "3"] } func observe() { Task { for await value in $items.values { print("values: ", value) } } $..
-
[Swift] ThreadSafe 한 Class 만들기Swift 2023. 10. 24. 13:26
Actor | Apple Developer Documentation Common protocol to which all actors conform. developer.apple.com Sendable | Apple Developer Documentation A type whose values can safely be passed across concurrency domains by copying. developer.apple.com 동시에 여러 쓰레드에서 하나의 인스턴스에 접근하면 어떻게 될까? 아무 조치도 취하지 않는다면 값은 이미 변했지만 변하지 않은 값을 돌려받을 수도 있다. 그래서 thread safe 하게 race condition 을 피해주어야한다. 그럴때 사용되는 것이 Actor 와 Send..
-
[Swift] DispatchQueue.global() 과 Task.detatched(priority: .background) 의 차이점Swift 2023. 10. 24. 12:01
Task | Apple Developer Documentation A unit of asynchronous work. developer.apple.com DispatchQueue.global().async { // background work } // struct 이기 때문에 변수에 할당 가능 let task = Task.detatched(priority: .background) { try await doSomethingBadAss() } Task { // task.result 로 결과를 예외 처리 가능 switch await task.result { case .success(let result): break case .failure(let error): break } // failure 처리 안할꺼면 ..
-
[Swift] Mirror 써보기Swift 2023. 10. 18. 15:51
class User: NSObject { // setValue 는 NSObject 의 method @objc var login: String = "raw" // string -> key 로 접근 가능 } func hasProperty(_ name: String, object: AnyObject) -> Bool { let mirror = Mirror(reflecting: object) for (label, _) in mirror.children { if label == name { return true } } return false } var user = User() // login: "raw" let parameters: [String: Any] = ["login": "octodog"] for (key,..
-
[Swift, Combine] AnyPublisher 를 async/await throws 로 사용해보자Swift 2023. 10. 4. 09:47
https://developer.apple.com/documentation/swift/withcheckedthrowingcontinuation(function:_:) withCheckedThrowingContinuation(function:_:) | Apple Developer DocumentationInvokes the passed in closure with a checked continuation for the current task.developer.apple.com import Foundation import Combine extension AnyPublisher where Failure: Error { var asyncThrows: Output { get async throws { try aw..