SWIFT

    [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 } 이렇게 함수를 ..

    [swift] Collection - indices

    별 내용 아니지만 몰랐던 방식이라 적어두기 :) for index in 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..

    [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..