1
내 객체는 SequenceType을 준수하므로 generate 함수를 구현합니다. 내 개체에 값 배열이있는 선택적 개체가 있습니다 배열이있는 경우 생성기를 통해 값을 반환하지만 값이없는 경우 생성자는 nil을 반환해야합니다. 아래 코드는 작동하지만 다소 장황하다. 이것을 처리 할 수있는 더 좋은 방법이 있습니까?swift sequenceType return nil
func generate() -> GeneratorOf<ValueType> {
if let values = self.data.values
{
var index : Int = 0
return GeneratorOf<ValueType> {
return (index < values.count) ? values[index++] : nil
}
}
//how to improve these lines (or wrap it into the code above?) This feels like repetition.
return GeneratorOf<ValueType> {
return nil
}
}