Publishers.ContainsWhere

μ œλ„€λ¦­ ꡬ쑰체 | 쑰건 ν΄λ‘œμ €λ₯Ό λ§Œμ‘±ν•˜λŠ” μš”μ†Œλ₯Ό μ „λ‹¬λ°›λŠλƒμ— 따라 λΆˆλ¦¬μ–Έ 값을 λ°©μΆœν•˜λŠ” Publisher

μ΄λ‹ˆμ…œλΌμ΄μ €λŠ” 두 개의 인자λ₯Ό λ°›λŠ”λ‹€.

  • upstream : μƒμœ„μ— 흐λ₯΄λŠ” Publisher

  • predicate : μš”μ†Œκ°€ 쑰건과 λ§žλŠ”μ§€ νŒλ‹¨ν•˜κΈ° μœ„ν•œ ν΄λ‘œμ €

predicate에 λ„˜κ²¨μ§„ ν΄λ‘œμ €λ₯Ό ν†΅κ³Όν•˜λŠ” μš”μ†Œκ°€ μžˆλ‹€λ©΄ trueλ₯Ό λ°˜ν™˜ν•˜κ³  그렇지 μ•ŠμœΌλ©΄ falseλ₯Ό λ°˜ν™˜ν•œλ‹€.

contains μ˜€νΌλ ˆμ΄ν„°μ™€ 관련이 μžˆλ‹€.

// Publishers.ContainsWhere Publisher
Publishers
  .ContainsWhere(upstream: Publishers.Sequence<[Int], Never>(sequence: [2, 4, 6])) { $0 == 5 }
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine ContainsWhere Error")
    case .finished:
      print("Combine ContainsWhere Finish")
    }
  }, receiveValue: { value in
    print("Combine ContainsWhere : \(value)")
  })
  .store(in: &cancellables)

// contains Operator
Publishers.Sequence<[Int], Never>(sequence: [2, 4, 6])
  .contains { $0 == 5 }
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine Contains Error")
    case .finished:
      print("Combine Contains Finish")
    }
  }, receiveValue: { value in
    print("Combine Contains : \(value)")
  })
  .store(in: &cancellables)

// Combine ContainsWhere : false
// Combine ContainsWhere Finish

ν΄λ‘œμ €λ‘œ 쑰건 평가식을 λ„˜κ²¨ ν•΄λ‹Ή 쑰건을 ν†΅κ³Όν•˜λŠ” μš”μ†Œκ°€ μžˆλŠ”μ§€μ— λŒ€ν•œ λΆˆλ¦¬μ–Έ 값을 λ°œν–‰ν•œλ‹€.

μƒμœ„ PublisherλŠ” 2, 4, 6의 값을 μˆœμ„œλŒ€λ‘œ λ‚΄λ©°, 쑰건인 $0 == 5λ₯Ό ν†΅κ³Όν•˜λŠ” μš”μ†Œκ°€ ν•˜λ‚˜λ„ μ—†μœΌλ―€λ‘œ 결과적으둜 falseλ₯Ό λ‚΄κ³  μ’…λ£Œν•œλ‹€.

RxSwift

ν•΄λ‹Ή κΈ°λŠ₯을 κ΅¬ν˜„ν•˜κΈ° μœ„ν•œ μ˜€νΌλ ˆμ΄ν„°λ₯Ό μ œκ³΅ν•˜μ§€ μ•ŠλŠ”λ‹€.

ReactiveSwift

ν•΄λ‹Ή κΈ°λŠ₯을 κ΅¬ν˜„ν•˜κΈ° μœ„ν•œ μ˜€νΌλ ˆμ΄ν„°λ₯Ό μ œκ³΅ν•˜μ§€ μ•ŠλŠ”λ‹€.

μ°Έκ³ 

ReactiveX - Operators - Contains

Last updated