아래의 대안 1이 완벽하게 작동하는 이유는 무엇입니까?
매크로는 물론 가짜이며 설명 목적으로 만 사용됩니다.
func commonPrefixLength<T: Swift.Collection, U: Swift.Collection where
T: Sequence, U: Sequence,
T.GeneratorType.Element: Equatable,
T.GeneratorType.Element == U.GeneratorType.Element>
(collection1: T, collection2: U) -> T.IndexType.DistanceType {
var collection2generator = collection2.generate()
var i: T.IndexType.DistanceType = 0
for element1 in collection1 {
#if alternative1
let element2 = collection2generator.next()
if (element1 != element2) {
return i
}
#elseif alternative2
let optionalElement2 = collection2generator.next()
if let element2 = optionalElement2 {
if (element1 != element2) {
return i
}
}
else {
break
}
#endif
i++
}
return i
}
commonPrefixLength("abX", "abc")
비교에서 선택 사항 ( element2
)과 비 선택 사항 ( )을 비교합니다 element1
.
if (element1 != element2) {
return i
}
선택 사항과 비 선택 사항을 비교하는 데 문제가 없습니다. 왜 있어야합니까? 경우 element2
이며 nil
, 그 위의 비교의 결과가 될 것입니다 true
. 잘 정의되어 있습니다.
선택 사항이 아닌 항목은 암시 적으로 선택 사항으로 캐스트 될 수 있습니다. 그렇지 않으면 선택 사항이 아닌 항목을 선택 사항에 할당 할 수 없습니다.
let nonOptional = ""
var optional: String? = nonOptional
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다