Swift 데이터 타입 2

정승호·2022년 5월 27일
0
post-thumbnail

Any

  • 변수 또는 상수의 데이터 타입이 Any로 지정되어 있다면
    스위프트의 모든 데이터 타입을 사용할 수 있습니다.

AnyObject

  • Any보다 한정된 의미로 class의 인스턴스만 할당할 수 있습니다.
var things: [AnyObject] = []
 
things.append(10)  // Argument type 'Int' expected to be an instance of a class
things.append(100) // Argument type 'Int' expected to be an instance of a class
things.append("Seogun") // Argument type 'String' expected to be an instance of a class
things.append(false)    // Argument type 'Bool' expected to be an instance of a class    
things.apeend(Some.init())
things.append({ print("서근개발노트") })  // Argument type '()->()' expected to be an instance of a class

** Any또는 AnyObject로 선언된 변수의 값을 가져다 쓰려면 매번 타입을 확인해야 하고 변환해줘야 하는 불편함을 초래할 수 있습니다.

nil

  • nil은 특정타입이 아니라 '없음'을 나타내는 Swift 키워드 입니다.
    (다른 언어의 NULL, Null, null 등과 같은 표현)
  • 변수 또는 상수에 값이 들어있지않고 비어있음을 나타내는데 사용합니다.
  • 변수 또는 상수에 값이 nil 이면 해당 변수 또는 상수에 접근했을 때 잘못된 메모리 접근으로 런타임 오류가 발생합니다. 이러한 오류를 해결하려면 옵셔널을 사용해야 합니다.

참고자료
https://allesbless.tistory.com/184

https://seons-dev.tistory.com/230

https://babbab2.tistory.com/128

0개의 댓글