iOS
[swift] @discardableResult 키워드
애플이 제공하는 어트리뷰트 키워드 중 하나인 discardableResult에 대한 포스팅입니다. discardableResult Apply this attribute to a function or method declaration to suppress the compiler warning when the function or method that returns a value is called without using its result. swift 공식 문서에는 이렇게 설명이 되어있습니다. 대충 함수의 return 값을 사용하지 않는데 경고가 뜨는 걸 막고 싶을 때 이걸 써라 ~ 이런 느낌 예시 출동 func add(x: Int, y: Int) -> Int { return x + y } 이렇게 함수를 ..
[iOS] isHidden에 대하여
생각없이 사용했던 isHidden이라는 프로퍼티에 대해 정리해보고자 글을 쓴다 공식문서 https://developer.apple.com/documentation/uikit/uiview/1622585-ishidden isHidden은 UIView의 Instance Property이다 뷰를 숨기고 싶을 때 이 값을 true로 해주면 뷰가 숨겨진다. Setting the value of this property to true hides the receiver and setting it to false shows the receiver. The default value is false. A hidden view disappears from its window and does not receive input e..
[Combine] AnyPublisher 배열 하나로 합치기 (MergeMany, collect)
개발하다보면 api 요청 한번으로 배열형태로 응답이 오는 경우도 있지만 하나의 api를 여러번 호출하여 배열로 합쳐서 사용해야하는 경우가 생긴다 func fetchData() -> AnyPublisher { // api 호출 } let result: AnyPublisher = Publishers .MergeMany( array.map { _ in fetchData() } ) .collect() .eraseToAnyPublisher() 이렇게 해주면 하나의 AnyPublisher에 배열 형태로 담아서 return 할 수 있게된다. https://developer.apple.com/documentation/combine/publishers/mergemany Apple Developer Documentatio..
[swiftUI] GeometryReader - 화면비율에 따라 UI 조절
https://developer.apple.com/documentation/swiftui/geometryreader#declaration Apple Developer Documentation developer.apple.com 별 설명이 없는 애플 공식문서 ... 혹시 자세히 설명되어있는 공식문서가 있다면 ... 댓글 부탁드립니다 😭 앱은 화면비율에 따라서 유동적으로 크기를 주는 경우가 많은데 swiftUI에서는 어떻게 처리하는지 알아보았습니다 ! struct ContentView: View { var body: some View { List { ForEach(0..
[Swift] if let 과 guard let
🎶 if let 과 guard let 오늘은 이 둘의 차이와 언제 어떤 걸 선택하여 사용하면 좋을지 공부해보았습니다. 🤔 if let let nickName: String? = "오뜨" if let nonOptionalNickname = nickName { print(nonOptionalNickname) } else { print("닉네임이 nil 값입니다.") } if let 의 작업방식(?) 은 다음과 같습니다. 1. nickName이 nil인지 체크한다. 2. nil이 아니라면 nonOptionalNickname이라는 상수에 언래핑된 nickName 값을 대입한다. -> 이 때 nonOptionalNickname의 타입은 String이지만 nickName의 타입은 여전히 String? 이다 3. n..
[iOS] main.swift 에 대하여 - Command Line Tool
코딩테스트를 연습하다보면 Playground를 이용하거나 입력값이 필요한 사이트의 경우 Command Line Tool을 사용하게 됩니다. Command Line Tool 템플릿으로 프로젝트를 생성하면 main.swift 파일이 기본적으로 추가되어 있습니다. 처음에는 main.swift 파일에서 문제를 풀고나서 그 다음 문제를 풀 때는 다른 swift 파일을 만들어서 문제를 풀었는데 이런 문장이 뜨면서 error 가 발생했습니다. Expressions are not allowed at the top level ? 이게 뭐지 ... 뭐지 ... 하고 찾아본 내용을 적어봅니다 애플리케이션 진입점 및 "main.swift" 앞서 대부분 의 앱 소스 파일에는 최상위 코드가 허용되지 않는다고 말했습니다. 예외는 ..
[Swift] class / struct - initializer - 조금 더 편하게 작성하는 법
# swift의 initializer에 대하여 - class에서 조금 더 간편하게 initializer 작성하는 방법 - struct / class의 initializer의 차이 오늘은 너무 기본이지만, 자세히 알고 있지 않았던 swift의 initializer에 대해 다뤄보겠습니다 swift에서 struct (구조체)의 경우에는 initializer를 따로 작성하지 않아도 자동으로 memberwise initializer를 제공해줍니다. struct Ohtt { let name: String let age: Int } // initializer를 작성해주지 않아도 // memberwise initializer를 제공해준다 Ohtt(name: "오뜨", age: 26) 하지만 class에서 initial..