[swift] Swift 딕셔너리에서 키와 값에 대한 조건문 사용 방법은?
다음은 Swift에서 딕셔너리의 키와 값에 대한 조건문을 사용하는 예제 코드입니다:
// 딕셔너리 생성
var dictionary = ["A": 1, "B": 2, "C": 3]
// 특정 키가 존재하는지 확인
if let value = dictionary["A"] {
print("Key 'A' exists with value \(value)")
} else {
print("Key 'A' does not exist")
}
// 특정 값이 존재하는지 확인
if dictionary.contains(where: { $0.value == 2 }) {
print("Value 2 exists in the dictionary")
} else {
print("Value 2 does not exist in the dictionary")
}
위의 코드에서는 dictionary
안에 키 “A”가 존재하는지 확인하고, 값 2가 존재하는지 확인하는 방법을 보여줍니다.
참고 문헌:
- Swift 문서: https://docs.swift.org/swift-book/LanguageGuide/CollectionTypes.html#ID108