본문 바로가기
반응형

iOS/Swift배우기7

Xcode에서 SwiftUI 코드선택 익숙해지기 1. 개요 안드로이드 스튜디오에서는 expand selection이라는걸 제공한다.커서기준으로 범위를 확장하면서 선택해주는 기능인데 flutter 개발할 때 매우 유용하다. 현재 내 커서가 위치한 위젯의 범위를 정확하게 선택가능기에 위젯들을 마치 레고 조립하듯이 코드를 작성할 수 있다. 헌데 Xcode에서는 그런 기능이 없다는것을 알았다... 너무 불편하다. 그래서 방법을 찾은 결과 결국 해답은 vim에 있었다. 2. 선택방법 여러가지 Va{ 혹은 Va}, vaBb 을 하면 플러터에서 하듯이 뷰의 전체를 선택가능하다 실제 vim에서는 vaBV를 해야한다. 고로 vaBV로 연습하자 va{ 혹은 va}, vaB을 하면 중괄호를 포함한 뷰의 바디부분을. 선택할 수 있다 vab는 소괄호 선택 vaB는 중괄호 선.. 2024. 3. 22.
[Swift] PropertyWrapper 사용법 https://docs.swift.org/swift-book/documentation/the-swift-programming-language/properties/ Documentation docs.swift.org 1. PropertyWrapper란? 파이썬의 데코레이션과 비슷하다. 파이썬의 데코레이션은 코드 앞뒤로 어떠한 코드를 붙여주는 작업을 해준다. 프로퍼티 래퍼또한 비슷하다. 어떤 프로퍼티 앞뒤로 특정한 작업을 넣을 수 있다. 2. 만드는법 만드는 방법은 간단하다. 1. struct를 정의할 때 @PropertyWrapper라고 적어준다 2. struct안에 wrappedValue라는 computedProperty를 정의해주면 된다 아래는 범위가 12까지인 프로퍼티를 프로퍼티 래퍼를 이용해 구현하.. 2024. 3. 22.
[SwiftUI] 자식에게 State 전달하기 https://developer.apple.com/documentation/swiftui/managing-user-interface-state Managing user interface state | Apple Developer Documentation Encapsulate view-specific data within your app’s view hierarchy to make your views reusable. developer.apple.com 1. Binding 키워드 먼저 자식View에다가 @Binding 이라는 프로퍼티 래퍼를 생성한다. 이 프로퍼티 래퍼는 부모의 스테이트를 받아오는 역할을 한다 struct PlayButton: View { @Binding var isPlaying: Bool.. 2024. 3. 21.
SwiftUI에서 {} 동작 원리 너무 궁금해서 찾아보았다.. 먼저 var body의 경우 아래와 같이 View라는 프로토콜에서 정의되어있고 computed property로 사용된다 그럼 VStack은 어떻게 동작하는가? https://stackoverflow.com/questions/66704988/returning-multiple-view-objects-from-a-closure-in-swift Returning multiple View objects from a closure in Swift I often see code like this: VStack { Text("A") Text("B") } From looking at the swift tutorials, I know that this is shorthand for speci.. 2024. 3. 20.
[Swift] Generic이란 1. Generic Function 아래와 같은 함수를 보자. 인트 변수를 두개바꿔주는 함수이다. func swapTwoInts(_ a: inout Int, _ b: inout Int) { let temporaryA = a a = b b = temporaryA } 만약 정수형이 아니라 실수형을 바꿔주고 싶다면? 아래처럼 함수바디 구현체는 같은데 함수이름과 파라미터 자료형만 다른걸 세개나 정의해줘야 한다. func swapTwoInts(_ a: inout Int, _ b: inout Int) { let temporaryA = a a = b b = temporaryA } func swapTwoStrings(_ a: inout String, _ b: inout String) { let temporaryA = .. 2024. 3. 20.
[Swift] inout 파라미터 1. inout 파라미터란 스위프트는 기본적으로 함수 파라미터가 constant다. 함수 내에서 이 값을 바꾸려 하면 컴파일 에러가 난다. 그럼 스위프트에서 call by reference를 어떻게 하냐? inout 파라미터를 사용하면 가능하다. inout 파라미터는 함수를 호출할 때 &를 붙여주어야 한다. C언어처럼 func swapTwoInts(_ a: inout Int, _ b: inout Int) { let temporaryA = a a = b b = temporaryA } var someInt = 3 var anotherInt = 107 swapTwoInts(&someInt, &anotherInt) print("someInt is now \(someInt), and anotherInt is no.. 2024. 3. 20.
[Swift] Protocol 배우기 https://docs.swift.org/swift-book/documentation/the-swift-programming-language/protocols/ Documentation docs.swift.org 1. 프로토콜이란? 그냥 다른언어 인터페이스랑 똑같다고 생각하면 된다. 그런데 Swift 이놈은 용어가 너무 다르다. 생긴건 아래와 같다 protocol SomeProtocol { var mustBeSettable: Int { get set } var doesNotNeedToBeSettable: Int { get } } protocol AnotherProtocol { static var someTypeProperty: Int { get set } } 프로토콜 안에있는 변수들은 Property R.. 2024. 3. 19.
반응형