의 DICT 키 또는 갱신 카운트를 만드는이 작업을 수행 할 수있는 간단한 방법이 있어야 것 같습니다 :한 줄 : 빠른
for each token
look it up in a dictionary
if it's there already, increment the value by 1
else create it and set value to 1
나는
for token in tokens {
if let count = myDict[token] {
myDict[token] = count + 1
} else {
myDict[token] = 1
}
하여이 작업을 수행 할 수 있습니다하지만 것 같아 이 일을 더 우아하고 단선 적으로 처리해야합니까? , 더 나은 아직 사용 무기 호 유착
for token in tokens {
myDict[token] = myDict[token] ? myDict[token]! + 1 : 1
}
:
FYI -'+ ='를'+'로 변경하십시오. – rmaddy
이 수정되었습니다. thanks rmaddy –