Skip to content
This repository was archived by the owner on Mar 26, 2025. It is now read-only.

Commit 8fa23bf

Browse files
committed
WordProcessorDelegate
1 parent d6305fb commit 8fa23bf

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

BirdProcessor/UI/ViewController.m

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#import "BirdProcessor-Swift.h"
22
#import "ViewController.h"
33

4-
@interface ViewController ()
4+
@interface ViewController () <WordProcessorDelegate>
55

66
@property (nonatomic) WordProcessor *wordProcessor;
77

@@ -17,6 +17,7 @@ - (void)viewDidLoad {
1717

1818
id<StringManipulatorProtocol> stringManipulator = [[StringManipulator alloc] init];
1919
self.wordProcessor = [[WordProcessor alloc] initWithStringManipulator:stringManipulator];
20+
self.wordProcessor.delegate = self;
2021
}
2122

2223
- (IBAction)didTapProcessButton:(UIButton *)sender {
@@ -27,4 +28,18 @@ - (IBAction)didTapProcessButton:(UIButton *)sender {
2728
[self.resultLabel setText:processingResult];
2829
}
2930

31+
// We’re only interested in the third method, but the compiler requires all three:
32+
33+
- (void)wordProcessor:(WordProcessor *)processor didBeginProcessingInput:(NSString *)string {
34+
35+
}
36+
37+
- (void)wordProcessor:(WordProcessor *)processor didFinishProcessingInput:(NSString *)string {
38+
39+
}
40+
41+
- (void)wordProcessor:(WordProcessor *)processor didEncounterError:(NSString *)error {
42+
// Show an error alert
43+
}
44+
3045
@end

BirdProcessor/WordProcessor/WordProcessor.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@
33

44
private let stringManipulator: any StringManipulatorProtocol
55

6+
@objc weak var delegate: (any WordProcessorDelegate)?
7+
68
@objc init(stringManipulator: any StringManipulatorProtocol) {
79
self.stringManipulator = stringManipulator
810
}
911

1012
@objc func processInputString(_ string: String) -> String {
13+
delegate?.wordProcessor(self, didBeginProcessingInput: string)
14+
// This runs *after* the resulting string is constructed
15+
// but *before* control is returned to the caller—
16+
// in other words, when processing is truly finished
17+
defer { delegate?.wordProcessor(self, didFinishProcessingInput: string) }
18+
1119
let isEmpty = stringManipulator.isEmpty(string)
1220
let isEmptyResult = isEmpty ? "is" : "is not"
1321

@@ -16,6 +24,10 @@
1624
"\(numberOfWords) word(s) and "
1725
} else { "" }
1826

27+
if numberOfWordsResult.isEmpty {
28+
delegate?.wordProcessor(self, didEncounterError: "Cannot count words")
29+
}
30+
1931
let numberOfCharacters = stringManipulator.numberOfCharacters(in: string)
2032
let numberOfCharactersResult = "\(numberOfCharacters) character(s)"
2133

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@objc protocol WordProcessorDelegate {
2+
func wordProcessor(_ processor: WordProcessor, didBeginProcessingInput string: String)
3+
func wordProcessor(_ processor: WordProcessor, didFinishProcessingInput string: String)
4+
func wordProcessor(_ processor: WordProcessor, didEncounterError error: String)
5+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ You’ll find different stages of the project in separate branches.
99
* [x] Implement the Objective-C protocol (`StringManipulatorProtocol`) in the Swift object (`StringManipulator`)
1010
* [x] Fix the crash that occurs when the non-implemented optional method is called
1111
* [x] Replace `WordProcessor` with a Swift version and use the better way to handle the optional method
12-
* [ ] Declare the `WordProcessorDelegate` protocol in Swift and implement it in Objective-C (`ViewController`)
12+
* [x] Declare the `WordProcessorDelegate` protocol in Swift and implement it in Objective-C (`ViewController`)
1313
* [ ] Mark the protocol’s methods as `optional` and remove the unneeded method implementations

0 commit comments

Comments
 (0)