From 5c73b1b1096424d59aab7194f9bc41132cf8be8a Mon Sep 17 00:00:00 2001 From: Imran Date: Tue, 24 May 2022 10:28:35 +0600 Subject: [PATCH] combine many operator --- .../CombieSwiftUI.xcodeproj/project.pbxproj | 4 ++ .../chapter3/chapter3 + Operators.swift | 25 +++++-- .../chapter4 + Filtering operator .swift | 72 +++++++++++++++++++ CombieSwiftUI/CombieSwiftUI/ContentView.swift | 8 +++ 4 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 CombieSwiftUI/CombieSwiftUI/Chapters/chapter4/chapter4 + Filtering operator .swift diff --git a/CombieSwiftUI/CombieSwiftUI.xcodeproj/project.pbxproj b/CombieSwiftUI/CombieSwiftUI.xcodeproj/project.pbxproj index 7725bf0..748e485 100644 --- a/CombieSwiftUI/CombieSwiftUI.xcodeproj/project.pbxproj +++ b/CombieSwiftUI/CombieSwiftUI.xcodeproj/project.pbxproj @@ -15,6 +15,7 @@ 1D3692CB283B905000B4494D /* extension + ContentView + 2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D3692CA283B904F00B4494D /* extension + ContentView + 2.swift */; }; 1D3692CD283B90E100B4494D /* chapter3 + Operators.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D3692CC283B90E100B4494D /* chapter3 + Operators.swift */; }; 1D3692D0283BA26400B4494D /* Chatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D3692CF283BA26400B4494D /* Chatter.swift */; }; + 1D3692D2283BF88400B4494D /* chapter4 + Filtering operator .swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D3692D1283BF88400B4494D /* chapter4 + Filtering operator .swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -27,6 +28,7 @@ 1D3692CA283B904F00B4494D /* extension + ContentView + 2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "extension + ContentView + 2.swift"; sourceTree = ""; }; 1D3692CC283B90E100B4494D /* chapter3 + Operators.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "chapter3 + Operators.swift"; sourceTree = ""; }; 1D3692CF283BA26400B4494D /* Chatter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Chatter.swift; sourceTree = ""; }; + 1D3692D1283BF88400B4494D /* chapter4 + Filtering operator .swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "chapter4 + Filtering operator .swift"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -104,6 +106,7 @@ 1D3692BB283A164E00B4494D /* chapter4 */ = { isa = PBXGroup; children = ( + 1D3692D1283BF88400B4494D /* chapter4 + Filtering operator .swift */, ); path = chapter4; sourceTree = ""; @@ -256,6 +259,7 @@ 1D3692CD283B90E100B4494D /* chapter3 + Operators.swift in Sources */, 1D3692AA283A157000B4494D /* ContentView.swift in Sources */, 1D3692CB283B905000B4494D /* extension + ContentView + 2.swift in Sources */, + 1D3692D2283BF88400B4494D /* chapter4 + Filtering operator .swift in Sources */, 1D3692A8283A157000B4494D /* CombieSwiftUIApp.swift in Sources */, 1D3692C6283A178000B4494D /* SupportCode.swift in Sources */, 1D3692D0283BA26400B4494D /* Chatter.swift in Sources */, diff --git a/CombieSwiftUI/CombieSwiftUI/Chapters/chapter3/chapter3 + Operators.swift b/CombieSwiftUI/CombieSwiftUI/Chapters/chapter3/chapter3 + Operators.swift index 7998037..f5bb7ef 100644 --- a/CombieSwiftUI/CombieSwiftUI/Chapters/chapter3/chapter3 + Operators.swift +++ b/CombieSwiftUI/CombieSwiftUI/Chapters/chapter3/chapter3 + Operators.swift @@ -9,11 +9,11 @@ import Foundation import Combine extension ContentView{ - + func collectingValues(){ var subscriptions = Set() - + SupportCode.example(of: "Collect") { ["A", "B", "C", "D", "E"].publisher .collect(2) @@ -51,7 +51,7 @@ extension ContentView{ print("\n---------> tryMap <---------") var subscriptions = Set() - Just("Directory name that does not exist") + Just("Directory name that does not exist") .tryMap { values in try FileManager.default.contentsOfDirectory(atPath: values) @@ -75,7 +75,7 @@ extension ContentView{ chat_one.message.value = "How's it going?" chat.value = chat_two - + chat .flatMap{ $0.message } .sink { chats in @@ -84,4 +84,21 @@ extension ContentView{ .store(in: &subscriptions) } + + func ReplacingUpstreamOutput(){ + print("\n---------> Replacing upstream output <---------") + + SupportCode.example(of: "Replacing upstream output") { + var subscriptions = Set() + + let replace = ["A", nil, "C"].publisher + + replace + .replaceNil(with: "-") + .map{ $0!} + .sink { item in + print(item) + }.store(in: &subscriptions) + } + } } diff --git a/CombieSwiftUI/CombieSwiftUI/Chapters/chapter4/chapter4 + Filtering operator .swift b/CombieSwiftUI/CombieSwiftUI/Chapters/chapter4/chapter4 + Filtering operator .swift new file mode 100644 index 0000000..b9757ae --- /dev/null +++ b/CombieSwiftUI/CombieSwiftUI/Chapters/chapter4/chapter4 + Filtering operator .swift @@ -0,0 +1,72 @@ +// +// chapter4 + Filtering operator .swift +// CombieSwiftUI +// +// Created by Imran on 23/5/22. +// + +import Foundation +import Combine + +extension ContentView{ + + func filterBasic(){ + SupportCode.example(of: "Filter Basic") { + var subscriptions = Set() + + let numbers = (1...100).publisher + numbers + .filter { $0.isMultiple(of: 3)} + .sink { item in + print(item) + } + .store(in: &subscriptions) + } + } + + func removeDuplicates(){ + SupportCode.example(of: "Remove Duplicates") { + var subscriptions = Set() + + let words = "hey hey there! want to listen to mister mister ha ha ha ha ?".components(separatedBy: " ").publisher + + words + .removeDuplicates() +// .map{ $0 } + .sink { item in + print(item ) + }.store(in: &subscriptions) + } + } + + func conpactMapValues(){ + SupportCode.example(of: "Compact Map") { + var subscriptios = Set() + + let stringValues = [" ", "", "", "", "", "i"].publisher + + stringValues + .compactMap{ ($0) } + .sink { item in + print(item) + } + .store(in: &subscriptios) + } + } + + func ignoreOutpuValues(){ + SupportCode.example(of: "IgnoreOutput") { + var subscribes = Set() + let numbersValues = (1...10_000).publisher + + numbersValues + .ignoreOutput() + .sink { errors in + print("Compled with: \(errors)") + } receiveValue: { item in + print(item) + } + .store(in: &subscribes) + } + } +} diff --git a/CombieSwiftUI/CombieSwiftUI/ContentView.swift b/CombieSwiftUI/CombieSwiftUI/ContentView.swift index abe5d42..eff04d8 100644 --- a/CombieSwiftUI/CombieSwiftUI/ContentView.swift +++ b/CombieSwiftUI/CombieSwiftUI/ContentView.swift @@ -21,6 +21,14 @@ struct ContentView: View { mappingValue() tryMapValue() flatMapValues() + ReplacingUpstreamOutput() + + print("\n---------> Chapter 4: Filtring Operators <---------") + + filterBasic() + removeDuplicates() + conpactMapValues() + ignoreOutpuValues() }