본문 바로가기

iOS21

[Swift] Dictionary(grouping:by:) https://developer.apple.com/documentation/swift/dictionary/init(grouping:by:) init(grouping:by:) | Apple Developer DocumentationCreates a new dictionary whose keys are the groupings returned by the given closure and whose values are arrays of the elements that returned each key.developer.apple.com 낯선 방식의 생성자이어서 기록해본다. let students = ["Kofi", "Abena", "Efua", "Kweku", "Akosua"]let studentsByLette.. 2024. 7. 12.
[Swift] KeyPath 이해하기 객체 속성에 접근할 때 타입 안전하게 접근하는 방법 : KeyPathSwift4에서 도입객체의 특정 속성에 대한 경로를 표현하는데 사용됨KeyPath를 통해 객체의 속성에 접근하면 컴파일 타임에 타입 체크가 이루어지기 때문에 안전한 코드 작성 가능KVO(Key-Value Observing)과 함께 사용되는 케이스가 있음struct Person { var name: String var age: Int}let nameKeyPath = \Person.namelet ageKeyPath = \Person.agelet person = Person(name: "John", age: 30)print(person[keyPath: nameKeyPath]) // "John"print(person[keyPath: .. 2024. 7. 11.
SwiftUI Tutorial 8 - Interfacing with UIKit 1. Create a view to represent a UIPageViewControllerUIViewControllerRepresentable 프로토콜을 준수하는 구조체를 만든다.UIViewControllerRepresentable이 요구하는 메서드를 구현한다.SwiftUI 는 뷰를 보여줄 준비가 되었을 때 makeUIViewController(context:)를 호출하고updateUIViewController는 해당 뷰 컨트롤러가 업데이트되어야 할 때마다 호출한다.import SwiftUIimport UIKitstruct PageViewController: UIViewControllerRepresentable { var pages: [Page] func makeUIViewControlle.. 2024. 7. 10.
SwiftUI Tutorial 7 - Composing complex interfaces 1. Add a category viewimport SwiftUIstruct CategoryHome: View { var body: some View { NavigationSplitView { Text("Hello, World!") .navigationTitle("Featured") } detail: { Text("Select a Landmark") } }} 2. Create a category listLandmark 구조체에 Category enum과 프로퍼티를 추가한다. struct Landmark: Hashable, Codable, Identifiable { var id: Int .. 2024. 7. 9.
SwiftUI Tutorial 6 - Animating views and transitions 1. Add hiking data to the app제공하는 json 데이터 파일을 프로젝트에 다운 받은 다음 데이터 양식에 맞게 구조체를 구현한다.struct Hike: Codable, Hashable, Identifiable { var id: Int var name: String var distance: Double var difficulty: Int var observations: [Observation] static var formatter = LengthFormatter() var distanceText: String { Hike.formatter .string(fromValue: distance, unit: .kilomete.. 2024. 7. 8.
SwiftUI Tutorial 5- Drawing paths and shapes 1. Create drawing data for a badge viewimport CoreGraphicsstruct HexagonParameters { struct Segment { let line: CGPoint let curve: CGPoint let control: CGPoint } static let adjustment: CGFloat = 0.085 static let segments = [ Segment( line: CGPoint(x: 0.60, y: 0.05), curve: CGPoint(x: 0.40, y: 0.05), control: CGPoint(x:.. 2024. 7. 7.
SwiftUI Tutorial 4 - Handling user input 1. Mark favorite landmarks모델 클래스에 isFavorite 프로퍼티를 추가한다.struct Landmark: Hashable, Codable, Identifiable { var id: Int var name: String var park: String var state: String var description: String var isFavorite: Bool private var imageName: String var image: Image { Image (imageName) } private var coordinates: Coordinates var locationCoordinate: CLLo.. 2024. 7. 6.
SwiftUI Tutorial 3 - Building lists and navigation 1. Create a landmark model먼저 튜토리얼 페이지에서 제공하는 데이터 파일들을 프로젝트에 다운 받은 후 데이터를 담을 구조체를 만든다.연산 프로퍼티인 image 와 locationCoordinate도 추후에 용이하게 써먹기 위해 만들어 놓는다.import Foundationimport SwiftUIimport CoreLocationstruct Landmark: Hashable, Codable { var id: Int var name: String var park: String var state: String var description: String private var imageName: String var image: Image { .. 2024. 7. 5.
SwiftUI Tutorial 2 - Creating and combining views 2 5. Use SwiftUi viewx from other frameworksMapKit 을 import 해서 Map 뷰를 써본다.6. Compose the detail view이제 custom한 뷰들을 조합한다. 눈에 띄는 것이 두가지. 첫째, Vstack(Hstack) 안에서 offset과 padding을 통해 다른 뷰의 영역 위로 이동할 수 있다. CircleImage가 MapView 위를 지나가는 것을 볼 수 있다.괜히 Stack 이라는 단어를 쓴 것이 아니었구나. 추측컨데 아래의 선언된 것이 위를 차지하는 듯하다. 둘째, 공통 속성일 경우 상위의 view 에서 속성을 지정할 수 있다. Text 뷰가 두개가 있지만 상위 뷰인 HStack에서 font와 foregroundStyle 속성을 선언할 수 .. 2024. 7. 4.