You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
vardic=[String: Int]()
for i in stringArray {dic[i]+=1}
dic[i] += 1은 딕셔너리에 해당 키가 없을 때 에러를 발생
vardic=[String: Int]()
for i in stringArray {
if let count =dic[i]{dic[i]= count +1}else{dic[i]=1}}
키가 딕셔너리에 있는지 확인하고, 존재하면 값을 증가시키며, 존재하지 않으면 값을 1 을 설정하도록 분기처리 할 수 있지만,
varfavoriteTVShows=["Red Dwarf","Blackadder","Fawlty Towers","Red Dwarf"]varfavoriteCounts=[String: Int]()
for show in favoriteTVShows {favoriteCounts[show, default:0]+=1}
dictionary default value 사용
dic[i] += 1
은 딕셔너리에 해당 키가 없을 때 에러를 발생1
을 설정하도록 분기처리 할 수 있지만,default
속성을 사용하는 방법이 더 간결하다.https://stackoverflow.com/questions/32415188/swift-dictionary-default-value
https://developer.apple.com/documentation/swift/dictionary/subscript(_:default:)
The text was updated successfully, but these errors were encountered: