nil
-
Swift Optional - 1Programming/Swift(iOS) 2021. 4. 2. 08:19
swift에서 Optional은 '값이 있을 수도 있고 없을 수도 있다.' 입니다. Optional은 nil을 안전하게 다루는 방법이라고 할 수 있습니다. 간단한 String optional입니다. var str1:String? = nil var str2:Optional = "" print(str1) print(str2) print(str1?.isEmpty) print(str2?.isEmpty) /* 결과 nil Optional("") nil Optional(true) */ 코드에서도 보듯이 optional의 nil과 isEmply와는 다릅니다. str1과 str2의 isEmpty의 결과를 잘 보세요. nil과 true가 나옵니다. 즉 optional에 nil이면 아예 값을 할당 조차 안했다는 얘기가 됩..