Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CombieSwiftUI/CombieSwiftUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -27,6 +28,7 @@
1D3692CA283B904F00B4494D /* extension + ContentView + 2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "extension + ContentView + 2.swift"; sourceTree = "<group>"; };
1D3692CC283B90E100B4494D /* chapter3 + Operators.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "chapter3 + Operators.swift"; sourceTree = "<group>"; };
1D3692CF283BA26400B4494D /* Chatter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Chatter.swift; sourceTree = "<group>"; };
1D3692D1283BF88400B4494D /* chapter4 + Filtering operator .swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "chapter4 + Filtering operator .swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -104,6 +106,7 @@
1D3692BB283A164E00B4494D /* chapter4 */ = {
isa = PBXGroup;
children = (
1D3692D1283BF88400B4494D /* chapter4 + Filtering operator .swift */,
);
path = chapter4;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import Foundation
import Combine

extension ContentView{

func collectingValues(){

var subscriptions = Set<AnyCancellable>()

SupportCode.example(of: "Collect") {
["A", "B", "C", "D", "E"].publisher
.collect(2)
Expand Down Expand Up @@ -51,7 +51,7 @@ extension ContentView{
print("\n---------> tryMap <---------")
var subscriptions = Set<AnyCancellable>()

Just("Directory name that does not exist")
Just("Directory name that does not exist")

.tryMap { values in
try FileManager.default.contentsOfDirectory(atPath: values)
Expand All @@ -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
Expand All @@ -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<AnyCancellable>()

let replace = ["A", nil, "C"].publisher

replace
.replaceNil(with: "-")
.map{ $0!}
.sink { item in
print(item)
}.store(in: &subscriptions)
}
}
}
Original file line number Diff line number Diff line change
@@ -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<AnyCancellable>()

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<AnyCancellable>()

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<AnyCancellable>()

let stringValues = [" ", "", "", "", "", "i"].publisher

stringValues
.compactMap{ ($0) }
.sink { item in
print(item)
}
.store(in: &subscriptios)
}
}

func ignoreOutpuValues(){
SupportCode.example(of: "IgnoreOutput") {
var subscribes = Set<AnyCancellable>()
let numbersValues = (1...10_000).publisher

numbersValues
.ignoreOutput()
.sink { errors in
print("Compled with: \(errors)")
} receiveValue: { item in
print(item)
}
.store(in: &subscribes)
}
}
}
8 changes: 8 additions & 0 deletions CombieSwiftUI/CombieSwiftUI/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ struct ContentView: View {
mappingValue()
tryMapValue()
flatMapValues()
ReplacingUpstreamOutput()

print("\n---------> Chapter 4: Filtring Operators <---------")

filterBasic()
removeDuplicates()
conpactMapValues()
ignoreOutpuValues()
}


Expand Down